score:2

Accepted answer

i was advised on this question. in such a situation the best approach is to use structural typing:

trait grand[t <: {def cloneobject: t}]

so that the user code might be the following:

case class person(name: string) {
  def cloneobject = copy()
}

object roll extends app with grand[person] {
...
}

score:1

clone() is from java.lang.object not from java.lang.cloneable. cloneable is just a tagging-interface.

what you are trying probably does not work because implementing cloneable does not force the implementor to override the protected object.clone() with a public one.

see http://docs.oracle.com/javase/7/docs/api/java/lang/object.html#clone() and http://docs.oracle.com/javase/7/docs/api/java/lang/cloneable.html

ln conclusion: no it is not possible to clone an object of an arbitrary type t1. you could however clone any object of type t2 where t2 is bound by a type with a public override of clone(). your example fails because object.clone() is protected, i.e. can only be called from within a subclass.


Related Query

More Query from same tag