Scala method clone in class Object cannot be accessed in T Access to protected method clone not permitted because

前端 未结 2 1550
有刺的猬
有刺的猬 2021-01-21 21:22

I\'ve got troubles with cloning in Scala. Is this possible to clone an object of an arbitrary type T? Something like this:

import collection.immutable.Stack

obj         


        
2条回答
  •  伪装坚强ぢ
    2021-01-21 22:21

    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] {
    ...
    }
    

提交回复
热议问题