ScalaMacros Boilerplate Examples

232 views
Skip to first unread message

Jesús López

unread,
Apr 25, 2012, 9:43:27 AM4/25/12
to scala...@googlegroups.com
Hello,

I am part of a spaniard group that is developing an embedded DSL over
Scala. It may be worth noting that we are far from being experts, we
have just been learning Scala the last few months. We have
experimented with the Scala basic syntax, but we have reached the
conclusion that there is some unavoidable boilerplate code that we do
not want to show to our clients. As a result, we need to use Code
Generation.

We are avoiding to start a Scala compiler plugin by now (because of
its attributed difficulty) so we have been reading about ScalaMacros. I have run
the (basic) examples shown in the SIPs and some of the macro test
cases existing in the Kepler project. Now I am searching how other
people use ScalaMacros in real cases to generate boilerplate, but I am
not finding examples. Where can I look for such macro implementations?

Greetings,
Jesús.

Heiko Seeberger

unread,
Apr 25, 2012, 10:17:56 AM4/25/12
to scala...@googlegroups.com

A simple example can be found here: https://github.com/hseeberger/scalalogging

Heiko


Jesús López

unread,
Apr 25, 2012, 11:50:57 AM4/25/12
to scala...@googlegroups.com
Thanks for your response. It's really interesting the way you implemented the log() function at LoggerMacros.

Anyway, I'd like to see further modifications of the AST. For example, is it possible to define a class/trait along with the macro expansion? If so, is it possible to do it with hygiene? I have been trying this task with the ClassDef at mirror, but I was unable to generate the Template argument.

Jesús.

Eugene Burmako

unread,
Apr 25, 2012, 11:55:47 AM4/25/12
to scala...@googlegroups.com
It should be possible to generate local classes, but exposing them to the outside program is a pain and will most likely not work. This is by design now, since current release focuses on macro definitions that operate locally. In one of the next releases we plan to add type macros that are capable of generating public types and have other parts of the program refer to them.

Speaking of your problems with ClassDef, what doesn't work? I spent a fair amount of time making sure that reify produces sane trees, so it'd be great to learn what is broken and to fix it.

Jesús López

unread,
Apr 25, 2012, 12:18:12 PM4/25/12
to scala...@googlegroups.com
Don't worry, the thing that doesn't work is me :) The documentation scarces and I have been trying to figure out how to invoke ClassDef by reading the code. The modifiers and the typeName are easy enough to understand, but I am unaware of the aim of that Template.

Jesús.


El miércoles, 25 de abril de 2012 17:55:47 UTC+2, Eugene Burmako escribió:
It should be possible to generate local classes, but exposing them to the outside program is a pain and will most likely not work. This is by design now, since current release focuses on macro definitions that operate locally. In one of the next releases we plan to add type macros that are capable of generating public types and have other parts of the program refer to them.

Speaking of your problems with ClassDef, what doesn't work? I spent a fair amount of time making sure that reify produces sane trees, so it'd be great to learn what is broken and to fix it.

Eugene Burmako

unread,
Apr 25, 2012, 12:24:22 PM4/25/12
to scala...@googlegroups.com
Template is a thingie that encapsulates bodies, supertypes and self-type of classes and objects.

If you're in doubt about how to construct a Template, or whatever else tree, try composing a snippet of the code that produces the thingie you're interested in, and then compile it with scalac -Xprint:typer -Yshow-trees <your file name>. This will print all the ASTs the compiler has after Typer, so you'll learn what exact shape you trees should have.
Reply all
Reply to author
Forward
0 new messages