Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I distribute a Scala macro as a project?

Suppose I have a Scala compile-time macro that I find useful and would like to share it (I do). How do I create a JAR file that when loaded into another project would execute the macro when compiling the new project?

Specifically, I've made a StaticAnnotation that rewrites the AST of the class that it wraps before compile time. This works in my Maven build (macro defined in the main directory, runs on test cases in the test directory) because I have

<compilerPlugins>
    <compilerPlugin>
        <groupId>org.scalamacros</groupId>
        <artifactId>paradise_2.10.5</artifactId>
        <version>2.1.0-M5</version>
    </compilerPlugin>
</compilerPlugins>

in my scala-maven-plugin. (I'm starting with a Scala 2.10 project and if it works, will provide both 2.10 and 2.11.)

But if I put the resulting JAR on a Scala console classpath, in a Scala script, or into another Maven project (without special compiler plugins), it simply ignores the macro: the AST does not get overwritten and my compile-time println statements don't execute. If I use the @compileTimeOnly annotation on my macro (new in Scala 2.11), then it complains with the @compileTimeOnly error message.

Do I really need to tell my users to add compiler plugins in their pom.xml files, as well as alternate instructions for SBT and other build tools? Other packages containing macros (MacWire, Log4s) don't come with complicated build instructions: they just say, "point to this dependency in Maven Central." I couldn't find the magic in their build process that makes this work. What am I missing?

like image 745
Jim Pivarski Avatar asked Jul 06 '15 00:07

Jim Pivarski


1 Answers

If you're relying on a macro-paradise-only feature then yes, you do need to tell your users to add compiler plugins. See http://docs.scala-lang.org/overviews/macros/annotations.html . The projects you mention are only using the scala compiler's built-in (non-paradise) macro features, not macro annotations.

like image 110
lmm Avatar answered Oct 15 '22 16:10

lmm