2

As an example, given the following Scala case classes...

case class A(str: String, num: Number)
case class B(as: Seq[A])
case class C(b1: B, b2: B)

... and an object hierarchy...

val c = C(B(Seq(A("SA",23),A("SB",42))),B(Seq()))

...I'd like to be able to do something like this:

val bindingString = "b1.as[1].num"
val value = get(c, bindingString)
assert value == 42

Certainly the core functionality required for this has already been implemented in a few libraries based on macros or reflection.

Is there already a library that offers just this? Or alternatively a part of an existing open source library which could be extracted?

sambe
  • 256
  • 1
  • 4
  • At some point you can use Scala dynamics ( http://stackoverflow.com/questions/15799811/how-does-type-dynamic-work-and-how-to-use-it ) or reflection/macro, as soon as you take care that it will be unsafe (not checked by compiler, can raise error at runtime). – cchantep Aug 07 '14 at 11:29
  • @applicius Thanks for the link. As far as I can see, this could be useful for replacing bindingString with a code-like representation, e.g. like `val value = dyn(c).b1.as(1).num`, but I'd still have to implement the dyn function and for that I would need some kind of case class reflection. – sambe Aug 07 '14 at 11:58
  • You may need look for a way to solve your final goal, the one you think require such 'dynamics', before go on the risky way of dynamics/reflection. – cchantep Aug 07 '14 at 12:07
  • The concrete use case is to allow the user to enter a filter string to filter against an arbitrary field. (In this case Scala dynamics are not useful) But as there are lots of libraries that work generically on the structure of case classes (e.g. Jerkson, Squeryl, ...), it would be useful to have the core of this ("case class reflection") in a single place, reusable for other generic code (e.g. for a generic TreeView similar to a debugger's view) – sambe Aug 07 '14 at 16:45
  • If you don't mind using Groovy: http://groovy.codehaus.org/GPath – sourcedelica Aug 08 '14 at 17:25

0 Answers0