- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
TraitName.this.T and objName.T are different
Sat, 2009-03-07, 18:41
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.
Sun, 2009-03-08, 01:27
#2
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.
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.
>
>