This file contains 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
import MyFuncMacro.myfunc | |
object MyFuncExample { | |
def main(args: Array[String]) { | |
val x: MyFunc[Int, Int] = myfunc { a: Int => a } | |
println(x) | |
println(x(42)) | |
} | |
} |
This file contains 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
import MyFuncMacro.myfunc | |
object MyFuncExample { | |
def main(args: Array[String]) { | |
val x: MyFunc[Int, Int] = myfunc { a: Int => a } | |
println(x) | |
println(x(42)) | |
} | |
} |
This file contains 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
scala> import scala.language.experimental.macros | |
import scala.language.experimental.macros | |
scala> import scala.reflect.macros.{ Context, TypecheckException } | |
import scala.reflect.macros.{Context, TypecheckException} | |
scala> object NoncompilationTests { | |
| def compiles(code: _): Boolean = macro compiles_impl | |
| def compiles_impl(c: Context)(code: c.Tree) = c.literal( | |
| try { |
This file contains 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
import sbt._ | |
import Keys._ | |
import Build.data | |
object build extends Build { | |
lazy val runAll = TaskKey[Unit]("run-all") | |
lazy val standardSettings = Seq( | |
runAllIn(Compile) | |
) |
This file contains 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
13:29 ~/Projects/Kepler_introduce-member/sandbox (topic/introduce-member)$ cat Macros.scala | |
import scala.reflect.macros.Context | |
import language.experimental.macros | |
object Macros { | |
def impl(c: Context)(target: c.Tree, name: c.Tree, code: c.Tree) = { | |
import c.universe._ | |
val Literal(Constant(targetType: Type)) = c.typeCheck(target) | |
val Literal(Constant(methodName: String)) = name | |
val Function(methodParams, methodBody) = code |
This file contains 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 s | |
object Test { | |
// Observe that x.companion is statically typed such that foo is callable | |
def f1() = { | |
val x = new Foo | |
println(x) // Foo instance | |
println(x.companion) // Foo companion | |
println(x.companion.foo) // I'm foo! |
This file contains 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
import language.experimental.macros | |
import language.implicitConversions | |
import scala.reflect.macros.Context | |
import scala.reflect.runtime.universe.Tree | |
class ReflectiveClosure[A, B](val tree: Tree, fn: Function1[A, B]) extends Function1[A, B] { | |
def apply(x: A) = fn(x) | |
} | |
object ReflectiveClosure { |
This file contains 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
import language.experimental.{ macros => scalaMacros } | |
import reflect.macros.Context | |
package object macros { | |
def safe[A](expr: A) = macro safeImpl[A] | |
def safeImpl[A](c: Context)(expr: c.Expr[A]): c.Expr[A] = { | |
import c.universe._ | |
def isIgnoredStatement(tree: Tree) = tree match { |
This file contains 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
import scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
import shapeless._ | |
object NatExample { | |
def toNat(n: Int): Any = macro toNat_impl | |
def toNat_impl(c: Context)(n: c.Expr[Int]) = { | |
import c.universe._ |
This file contains 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
/** Constructing "singleton types" from compile-time literals | |
* | |
* val x = Example.foo(42) | |
* val y: x.T = 42 // compiles | |
* val z: x.T = 43 // doesn't | |
* | |
*/ | |
package s | |
import scala.language.experimental.macros |