0

Given the simple example source I need to be able to create a copy constructor and implicit conversion method. Can somebody please advice how to achieve this with Scala 2.10 and new macros feature?

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?
  }

}
jdevelop
  • 12,176
  • 10
  • 56
  • 112
  • 2
    It's not clear to me what you want here, or why a macro is required to achieve it. – Daniel C. Sobral Feb 25 '13 at 02:16
  • I have object of type Entity and I need to pass the object to method which expects type "Entity with Storable". So I need to take that value of Entity and create new value of type Entity with Storable - copy all fields from Entity and add some value to mixing field "StorageId". Does this make it all clear? – jdevelop Feb 25 '13 at 04:02
  • possible duplicate of [Mixing in a trait dynamically](http://stackoverflow.com/questions/10373318/mixing-in-a-trait-dynamically) – Daniel C. Sobral Feb 25 '13 at 11:00
  • Turns out this is a duplicate (and I was wrong). See the linked question. – Daniel C. Sobral Feb 25 '13 at 11:01

0 Answers0