1

Given a case class

case class Foo(int: Int, str: String)

I would like this interface (or equivalent)

Foo.addField[Boolean]("bool").as("Foo2")

to automatically generate the code

case class Foo2(int: Int, str: String, bool: Boolean)

Is this possible using scala macros? I've had a look around and I can't find any decisive answer out there. Unfortunately I'm not too familiar with the API.

I'm not concerned about methods or anything like that, I just want a new named bucket the same as the previous one with one or more new fields.

EDIT: I'm pretty sure this blog post http://ravellaw.com/using-macro-annotations-to-reduce-boilerplate/ provides all the pieces of the jigsaw I need.

Gesar
  • 389
  • 1
  • 7
  • 1
    Does http://stackoverflow.com/a/22857231/6345611 help in your case? – dveim Jan 25 '17 at 16:49
  • It's kind of half the answer. I know from that that in principle I can create a new case class from arguments passed in but I'm not sure about the introspection necessary to get the list of arguments in the existing case class. Plus I think there must be a more principled way of generating a case class than just passing a string of the code to export, but I might be wrong. – Gesar Jan 25 '17 at 16:58
  • 1
    I don't think this is possible as you state it. _Maybe_ with a compiler plugin. – Michael Zajac Jan 26 '17 at 00:46

0 Answers0