As you suggested, abstract types or general parameters are what you need. Do you need MyDataStructure not to be a sign or an abstract class? The following defines that MyDataStructure is an abstract class, but you can also make it a trait.
abstract class MyDataStructure { type T def myClone: T } class MyDataStructureExtended(foo: String) extends MyDataStructure { type T = MyDataStructureExtended def myClone = new MyDataStructureExtended(foo) }
Scala interpreter results show that the myClone method defined in MyDataStructureExtended is the correct type.
scala> val mde = new MyDataStructureExtended("foo") val mde = new MyDataStructureExtended("foo") mde: MyDataStructureExtended = MyDataStructureExtended@3ff5d699 scala> val cloned = mde.myClone val cloned = mde.myClone cloned: MyDataStructureExtended = MyDataStructureExtended@2e1ed620
You might want to restrict T so that its type can only be a subclass type of MyDataStructure
abstract class MyDataStructure { type T <: MyDataStructure def myClone: T }
I do not know your requirements, but I believe that Scala 2.8 will have nice functionality with case classes and named arguments that allow you to clone case classes using the copy method.
faran
source share