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

Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
  • I am open minded to any solution, I guess it may be possible to use macros right? – Sebastien Lorber Feb 25 '13 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`)? – Régis Jean-Gilles Feb 25 '13 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, (https://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. – Leo Feb 25 '13 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... – Sebastien Lorber Feb 26 '13 at 00:01
  • Then you might want to have a look at my solution to this similar question: http://stackoverflow.com/a/12521080/1632462 – Régis Jean-Gilles Feb 26 '13 at 00:30
  • Thanks for the conversion trick, but my subclasses do not have the same field. So I did implement the copy on each subclass. Is it possible to avoid implemention in each subclass having different fields? (meaning the copy method doesn't have the same signature for each subclass I guess) – Sebastien Lorber Feb 26 '13 at 01:36
  • 2
    See my answer [here](http://stackoverflow.com/a/13447439/334519), which uses macros to do what I think you're looking for, and works with 2.10.0. – Travis Brown Feb 26 '13 at 02:40
  • Scala does not have dependent types. There's no way to do what you're looking for (if I understand you to want particular property values to become part of the type in which they appear). – Randall Schulz Feb 26 '13 at 03:14
  • @Sebastien Lorber: if your case classes have different argument lists then my solution is not applicable, and I think the actual solution (other than manually implementing copy in each class) can only come from a macro (or compiler plugin). Travis Brown's macro looks like a nice contender. – Régis Jean-Gilles 18 mins ago – Régis Jean-Gilles Feb 26 '13 at 08:08

1 Answers1

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?

Community
  • 1
  • 1
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419