This page is no longer maintained — Please continue to the home page at www.scala-lang.org

TraitName.this.T and objName.T are different

2 replies
Mushtaq Ahmed
Joined: 2009-03-07,
User offline. Last seen 30 weeks 2 days ago.

I am unable to find how to overcome this error:

trait A {
type T <: A
val value: T
}

trait B { self: A =>
def maker(a: T): B
def test(b: B) = b.maker(value) //compile error: type mismatch; found:
B.this.T required b.T
}

Please help.

Kris Nuttycombe
Joined: 2009-01-16,
User offline. Last seen 42 years 45 weeks ago.
Re: TraitName.this.T and objName.T are different

In your example, T remains abstract. If you make it concrete, it works:

scala> trait A {
| type T <: A
| val value: T
| }
defined trait A

scala> trait B { self: A =>
| type T = A
| def maker(a: T): B
| def test(b: B) = b.maker(value)
| }
defined trait B

scala>

Kris

On Sat, Mar 7, 2009 at 10:38 AM, Mushtaq Ahmed wrote:
>
> I am unable to find how to overcome this error:
>
>  trait A {
>    type T <: A
>    val value: T
>  }
>
>  trait B { self: A =>
>    def maker(a: T): B
>    def test(b: B) = b.maker(value) //compile error: type mismatch; found:
> B.this.T required b.T
>  }
>
> Please help.
> --
> View this message in context: http://www.nabble.com/TraitName.this.T-and-objName.T-are-different-tp223...
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: TraitName.this.T and objName.T are different

On Sat, Mar 07, 2009 at 09:38:18AM -0800, Mushtaq Ahmed wrote:
> I am unable to find how to overcome this error:
>
> trait A {
> type T <: A
> val value: T
> }
>
> trait B { self: A =>
> def maker(a: T): B
> def test(b: B) = b.maker(value) //compile error: type mismatch; found:
> B.this.T required b.T
> }

The way you have written it, you are passing one B (refer to it as B1
for clarity) into another B (call it B2, it's posing as an A) and
calling a method on B1 (which expects an argument of type B1.T) but
passing an argument whose type is B2.T. Since there's no guarantee
these are the same thing, that's an error.

Write it this way:

def maker(a: A#T): B

...which defines the parameter type as any A's T, not just this
particular A's T.

On Sat, Mar 07, 2009 at 11:46:18AM -0700, Kris Nuttycombe wrote:
> In your example, T remains abstract. If you make it concrete, it works:

It compiles, but it also presumably defeats the purpose of having an
abstract type.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland