7

I'm trying to do something like that:

trait IdentifiableModel[T] { self: { def copy(id: ObjectId): T } => val id: ObjectId } 

I've found some other related questions trying to do similar things but they didn't really answer to this question. In my case, I'm trying to copy the case class subclasses of IdentifiableModel that all share an id value

9
  • I am open minded to any solution, I guess it may be possible to use macros right? Commented Feb 25, 2013 at 23:00
  • What is it exactly that you want to achieve? Tell me if I'm correct: you have a set of case classes with one unique id field and want to be able to call the automatically generated copy method polymorphically (that is, through their common base class IdentifiableModel)? Commented Feb 25, 2013 at 23:06
  • This could be achieved through the use of implicit macro, i.e. having an implicit conversion from a case class T instance having an id: ObjectId field into an IdentifiableModel[T] { def copy(id: ObjectId): T }. In scala 2.10.1, due to a bug in implicit macros, (issues.scala-lang.org/browse/SI-5923), you'll have to use a trick with existential types, unless you can wait for 2.10.2, it seems. I'd be interested in knowing more about your complete use case though, to check it'll work as mentioned above. Commented Feb 25, 2013 at 23:50
  • @RégisJean-Gilles yes, so that a "generic model handler" can call this copy method on all subclasses of IdentifiableModel. What I've done is adding a specific copy implementation on all subclasses actually... Commented Feb 26, 2013 at 0:01
  • 2
    See my answer here, which uses macros to do what I think you're looking for, and works with 2.10.0. Commented Feb 26, 2013 at 2:40

1 Answer 1

1

Travis Brown has the good answer but didn't reply.

He uses Scala macros: Howto model named parameters in method invocations with Scala macros?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.