tags:

views:

128

answers:

2

Hello,

could you explain me what is incorrect in this trivial example?

class C1 (val id: Int) 

abstract class C2 [T] {
    def m1 [T] 
}

class C3 [C1] extends C2
{
    override 
    def m1 (obj: C1) {
        println (obj.id)        
    }           
}

I have got: value id is not a member of type parameter C1 Why?

+6  A: 

By writing class C3[C1] you're saying that C3 takes a type parameter called C1. So inside the class definition of C3 the classname C1 refers to that typeparameter, not the class C1.

What you probably meant to write was class C3 extends C2[C1] (i.e. you're passing the class C1 as the type parameter for C2).

sepp2k
Thank you for you suggestion.I rewrite C3 toclass C3 extends C2 [C1]{ override def m1 [C1](obj: C1) { println (obj.id) } }But I still have the same error.
kolchanov
@kolchanov That's because you did the same thing. If you write `def m1[C1]`, then you are saying `C1` is a type parameter -- some arbitrary type that will be provided at use-site -- which will _shadow_ the actual `C1` type.
Daniel
+4  A: 

There is several things at play in your example. Here is the example modified:

class C1 (val id: Int) 

abstract class C2 [T] {
  // don't repeat [T] and the method takes an arg
  // within C2, T refers to the type parameter
  def m1(t:T) 
}

class C3 extends C2[C1] {  // if in REPL put the brace on the same line
  // no override when implementing abstract 
  def m1(obj:C1) { println(obj.id) } 
}

This should compile (and run in the REPL).

huynhjl
Thank you.This is exactly what I want :)
kolchanov