Created
July 22, 2014 09:43
-
-
Save fancellu/1020d04e7972cb78d328 to your computer and use it in GitHub Desktop.
Simple Macro example, allows you to have a string which shows when last compiled. Remember that your main has to be in a different project to the macro (for the moment)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.felstar.macros.timestamp | |
import scala.reflect.macros.blackbox.Context | |
import scala.language.experimental.macros | |
object TimestampMacro { | |
def timestampString: String = macro timestampMacro | |
def timestampMacro(c: Context): c.Tree = { | |
import c.universe._ | |
val now = new java.util.Date().toString | |
q"$now" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object TimestampMain extends App { | |
val timestamp=com.felstar.macros.timestamp.TimestampMacro.timestampString | |
println(s"This was compiled at ${timestamp}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment