Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created July 22, 2014 09:43
Show Gist options
  • Save fancellu/1020d04e7972cb78d328 to your computer and use it in GitHub Desktop.
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)
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"
}
}
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