Does anyone know what's going on here with this compiler error? The error goes away if I don't extend INode.
trait AbsTypes
{
type TKey
type TValue
}
trait INode extends AbsTypes
{
def get(key : TKey) : TValue
def set(key : TKey, v : TValue) : INode
}
class ANode[TKey,TValue](
val akey : TKey,
val aval : TValue
) extends INode
{
// ERROR : type mismatch; found : ANode.this.aval.type (with underlying type TValue) required: ANode.this.TValue
def get(key : TKey) : TValue = { aval }
def set(key : TKey, v : TValue) : INode = {
new ANode(key,v)
}
}
Generic parameters don't automatically override abstract types, even if they have the same names. Try renaming the generic parameters (to avoid name conflicts), and then declaring the types TKey
and TValue
in the method body.
class ANode[A,B](
val akey : A,
val aval : B
) extends INode {
type TKey=A
type TValue=B
def get(key : TKey) : TValue = aval
def set(key : TKey, v : TValue) : INode = new ANode(key,v)
}
I suppose it would be nice if the compiler emitted an error on the line where you specified the names of the generic types, instead of waiting until you started using those types.