We have been banging our heads for a while on this but we cannot find a solution.
In our project we would like to write some DSL to migrate some old code in our codebase.
We would like to make a macro that given an instance of a case class gives us the possibility to extract the value in a typesafe manner. In this case it should be possible to declare x of type Int.
val someVariable = MyPersonalCaseClass(123, "SOMESTRING")
val x = Macros.->(someVariable, "token")
def ->[T](value:T,key: String): Any = macro MacrosImpl.arrow[T]
We assumed that the only way was with whitebox macros, feel free to change the signatures.
Just to make sure I understand what you’re looking for:
val someVariable = MyPersonalCaseClass(123, "SOMESTRING")
val x = Macros.->(someVariable, "token")
This is for a structure like this?
case class MyPersonalCaseClass(token: Int, str: String)
More importantly, “token” is a compile-time constant, referring to the field name, correct? (More on this below.)
Assuming that it’s correct – do you have a specific reason why you want to write your own macro? Similar functionality to this is available out of the box using a lens library like Monocle, and you could certainly build this and far more using Shapeless. Both are probably easier than rolling your own macro.
If the string “token” isn’t a compile-time constant, though, this is pretty obviously impossible – if the field name isn’t known at compile-time, then there is no way to know at compile-time what type it resolves to.
It might be easier to give a good answer if you can explain the intended use case…
The difference is the existing “programmers” user-experience. They have a lot of experience with Perl language but they are not programmers. Retraining them would cost a lot more than getting some simple macros.