Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
@travisbrown
travisbrown / Main.scala
Last active December 14, 2015 14:29 — forked from puffnfresh/Main.scala
import MyFuncMacro.myfunc
object MyFuncExample {
def main(args: Array[String]) {
val x: MyFunc[Int, Int] = myfunc { a: Int => a }
println(x)
println(x(42))
}
}
@puffnfresh
puffnfresh / Main.scala
Created March 6, 2013 02:44
Implementing a Single Abstract Method from a Scala lambda using macros (SAMBDAS!)
import MyFuncMacro.myfunc
object MyFuncExample {
def main(args: Array[String]) {
val x: MyFunc[Int, Int] = myfunc { a: Int => a }
println(x)
println(x(42))
}
}
@travisbrown
travisbrown / noncompilation.scala
Last active January 13, 2016 18:00
Testing for compiler errors with untyped macros.
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 {
@gseitz
gseitz / build.scala
Created February 19, 2013 22:52
SBT task for running all main classes
import sbt._
import Keys._
import Build.data
object build extends Build {
lazy val runAll = TaskKey[Unit]("run-all")
lazy val standardSettings = Seq(
runAllIn(Compile)
)
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
@paulp
paulp / test.scala
Created February 1, 2013 17:57
statically typed companion objects
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!
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 {
@puffnfresh
puffnfresh / Macros.scala
Last active December 11, 2015 01:58
Removing any2unit via a macro.
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 {
@travisbrown
travisbrown / nat-example.scala
Created January 7, 2013 12:26
Creating a Nat type from a compile-time literal
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._
/** 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