Examples from my repo https://github.com/BBartosz/scala-macros-examples ported to new inline macros in scalameta
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
macros
project
src/main/scala
.gitignore
README.md
build.sbt

README.md

scalameta-macros-examples

http://www.bbartosz.com/blog/2016/09/16/new-scalameta-inline-macros-examples/

Examples from my repo https://github.com/BBartosz/scala-macros-examples ported to new inline macros in scalameta

@main example:

@main
object Boot {
 
}

expands to:

object Boot extends App {

}

@Benchmark example:

  @Benchmark
  def testMethod[String]: Double = {
    val x = 2.0 + 2.0
    Math.pow(x, x)
  }

expands to:

  def testMethod[String]: Double = {
    val start = System.nanoTime()
    val result = {
      val x = 2.0 + 2.0
      Math.pow(x, x)
    }
    val end = System.nanoTime()
    println("testMethod elapsed time: " + (end - start) + "ns")
    result
  }

@RetryOnFailure(n: Int) example:

  @RetryOnFailure(20)
  def failMethod[String](): Unit = {
    val random = Random.nextInt(10)
    println(s"evaluating random= $random")
    utils.methodThrowingException(random)
  }

expands to:

  def failMethod[String](): Unit = {
    import scala.util.Try

    for( a <- 1 to 20){
      val res = Try{
        val random = Random.nextInt(10)
        println(s"evaluating random= $random")
        utils.methodThrowingException(random)
      }
      if(res.isSuccess){
        return res.get
      }
    }
    throw new Exception("Method fails after 20 repeats")
  }

@Mappable example:

@Mappable
case class Car(color: String, model: String, year: Int, owner: String){
  def turnOnRadio = {
    "playing"
  }
}

Expands to:

case class Car(color: String, model: String, year: Int, owner: String){
  def turnOnRadio = {
    "playing"
  }
  
  def toMap: Map[String, Any] = Map("color" -> color, "model" -> model, "year" -> year, "owner" -> owner)
}