Couple of questions on scala abstract types.
- Do I have to use parameterized [] types if I want to use the type in a constructor value? ie. is it possible to have a class with abstract constructor parameter types? If I get rid of [T1,T2] and use INode#TKey and INode#TValue as the constructor parameter types, what am I doing? I get confusing error messages.
- How do I solve this nicely without resorting to inner types? My use of INode in the definitions seem to be implying I could return / receive an INode with different types for TKey & TValue. How do I restrict it to the same TKey/TValue types as my current type, without restricting myself to returning/receiving exactly "this" instance?
trait AbsTypes
{
type TKey
type TValue
}
trait INode extends AbsTypes
{
def get(key : TKey) : TValue
def set(key : TKey, v : TValue) : INode
def combine(other : INode, key : TKey): INode
}
class ANode[T1,T2](
val akey : T1,
val aval : T2
) extends INode
{
type TKey = T1
type TValue = T2
type Node = ANode[T1,T2]
def get(key : TKey) : TValue = { aval }
def set(key : TKey, v : TValue) : INode = {
new ANode(key,v)
}
def combine(other : INode, key :TKey) : INode = {
//ERROR : type mismatch; found : key.type (with underlying type ANode.this.TKey) required: other.TKey
other.set(key, aval)
}
}