1

I'm just getting familiar with Scala Macros.

How can I do the following (or is this possible):

 case class Foo(name:String)

 //'keys' is some macro implementation

 val fooKeys = keys[Foo] //Returns object FooKeys{ val name = "name" }

 println(fooKeys.name) // "name"

I don't mind if the definition is local - so long as the type is defined and available.

ed.
  • 2,696
  • 3
  • 22
  • 25
  • 1
    You can't create new types using macros (it's possible with type macros, which are in paradise, but type macros aren't going to be included in any Scala version according to a conversation I had with Eugene Burmako on IRC). This question might help though: http://stackoverflow.com/questions/17326661/scala-macro-define-top-level-object – Ionuț G. Stan Jul 31 '13 at 13:44
  • Cheers Ionuț - wasn't clear on whether it was possible - I saw you could create anonymous inner classes but I guess that won't suffice. – ed. Jul 31 '13 at 13:53
  • 1
    If you're willing to settle for a structural type this is pretty easy—see for example my question [here](http://stackoverflow.com/q/14370842/334519) and blog posts [here](http://meta.plasm.us/posts/2013/06/19/macro-supported-dsls-for-schema-bindings/) and [here](http://meta.plasm.us/posts/2013/07/11/fake-type-providers-part-2/). You could even use [vampire methods](http://meta.plasm.us/posts/2013/07/12/vampire-methods-for-structural-types/) to avoid the reflective access hit that normally comes with structural types. – Travis Brown Jul 31 '13 at 13:54
  • 2
    I probably wanted to say that type macros are just very unlikely to make it in any Scala version (and definitely not in 2.11), but wasn't careful enough with words. Sorry for the confusion. There are two reasons for that: a) their type expansion feature doesn't pull its weight as per https://groups.google.com/d/topic/scala-internals/91W0-PxMQ9Q/discussion, b) their code generation feature is hands down beaten by macro annotations. However this doesn't mean that macros will never be able to generate new types (take, for example, macro annotations that are tentatively scheduled for 2.12). – Eugene Burmako Jul 31 '13 at 15:39

0 Answers0