0

给定简单的示例源,我需要能够创建复制构造函数和隐式转换方法。有人可以建议如何使用 Scala 2.10 和新的宏功能来实现这一点吗?

import java.util.UUID

object Sample {

  case class Entity(id: UUID, name: String)

  trait Storable {

    val storageId: UUID

  }

  trait Storage {

    type T <: Entity with Storable

    def save(src: T) : T

  }

  class StorageImpl extends Storage {

    type T = Entity with Storable

    def save(src: StorageImpl#T): StorageImpl#T = null
  }

  def getEntity() = Entity(UUID.randomUUID(), "Test")

  def main(args: Array[String]) {
    val storage = new StorageImpl
    val entity = getEntity() // get this from some third-party module
    storage.save(entity) // HOW?! Create copy constructor and implicit?
  }

}
4

0 回答 0