Skip to content

Instantly share code, notes, and snippets.

@squito
Last active January 28, 2016 07:41

Revisions

  1. squito revised this gist Oct 25, 2013. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions dynamicTraitGoal.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    trait SomeData {
    def x: Int
    def y: Float
    def z: Float
    }

    trait OtherData {
    def a: Float
    def b: Int
    }

    @ByteBufferBacked(classOf[SomeData]) class SomeDataImpl
    @ByteBufferBacked(classOf[OtherData]) class OtherDataImpl
  2. squito revised this gist Oct 22, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion quasiParentClass.scala
    Original file line number Diff line number Diff line change
    @@ -1 +1,2 @@
    val q"class $ignore extends $addedType" = q"class Foo extends com.imranrashid.oleander.macros.SimpleTrait"
    val q"class $ignore extends $addedType" =
    q"class Foo extends com.imranrashid.oleander.macros.SimpleTrait"
  3. squito revised this gist Oct 22, 2013. 14 changed files with 0 additions and 0 deletions.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
    File renamed without changes.
  4. squito revised this gist Oct 22, 2013. 4 changed files with 7 additions and 0 deletions.
    1 change: 1 addition & 0 deletions quasiClassDef
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    val classdef = q"""class Foo extends A with B with C {}"""
    1 change: 1 addition & 0 deletions quasiClassDefUnapply
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    val q"class $cname extends $parent with ..$traits { ..$body }" = classdef
    1 change: 1 addition & 0 deletions quasiParentClass
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    val q"class $ignore extends $addedType" = q"class Foo extends com.imranrashid.oleander.macros.SimpleTrait"
    4 changes: 4 additions & 0 deletions reifyDefDefs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    val newDefDefs = reify {
    def x = 5
    def y = 7.0f
    }.tree match { case Block(defs, _) => defs}
  5. squito revised this gist Oct 22, 2013. 8 changed files with 64 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions addedTrait
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    val addedTrait = Select(Select(Select(
    Select(Ident(newTermName("com")), newTermName("imranrashid")),
    newTermName("oleander")),newTermName("macros")),
    newTypeName("SimpleTrait"))
    6 changes: 6 additions & 0 deletions annotations_goal
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    trait SimpleTrait {
    def x: Int
    def y: Float
    }

    @FillTraitDefs class Foo extends SimpleTrait {}
    29 changes: 29 additions & 0 deletions full_macro
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    class FillTraitDefs extends StaticAnnotation {
    def macroTransform(annottees: Any*) = macro SimpleTraitImpl.addDefs
    }

    object SimpleTraitImpl {

    def addDefs(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
    import c.universe._
    val inputs = annottees.map(_.tree).toList
    val newDefDefs = List(
    DefDef(Modifiers(), newTermName("x"), List(), List(), TypeTree(), Literal(Constant(5))),
    DefDef(Modifiers(), newTermName("y"), List(), List(), TypeTree(), Literal(Constant(7.0f)))
    )
    val modDefs = inputs map {tree => tree match {
    case ClassDef(mods, name, something, template) =>
    val q = template match {
    case Template(superMaybe, emptyValDef, defs) =>
    Template(superMaybe, emptyValDef, defs ++ newDefDefs)
    case y =>
    y
    }
    ClassDef(mods, name, something, q)
    case x =>
    x
    }}
    val result = c.Expr(Block(modDefs, Literal(Constant())))
    result
    }
    }
    2 changes: 2 additions & 0 deletions quasiDefDef
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    scala> q"def x = 5"
    res3: reflect.runtime.universe.DefDef = def x = 5
    5 changes: 5 additions & 0 deletions quasiReplImports
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    import language.experimental.macros
    import reflect.macros.Context
    import scala.annotation.StaticAnnotation
    import scala.reflect.runtime.{universe => ru}
    import ru._
    2 changes: 2 additions & 0 deletions reify
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    scala> ru.showRaw{ru.reify{def x = 5}}
    res11: String = Expr(Block(List(DefDef(Modifiers(), newTermName("x"), List(), List(), TypeTree(), Literal(Constant(5)))), Literal(Constant(()))))
    4 changes: 4 additions & 0 deletions repl_imports
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    import language.experimental.macros
    import reflect.macros.Context
    import scala.annotation.StaticAnnotation
    import scala.reflect.runtime.{universe => ru}
    12 changes: 12 additions & 0 deletions showRaw
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    // AST of defining a val
    scala> ru.showRaw{ru.reify{val x = 5}}
    res1: String = Expr(Block(List(ValDef(Modifiers(), newTermName("x"), TypeTree(), Literal(Constant(5)))), Literal(Constant(()))))

    // AST for defining a class
    scala> ru.showRaw{ru.reify{class B}}
    res2: String = Expr(Block(List(ClassDef(Modifiers(), newTypeName("B"), List(), Template(List(Ident(newTypeName("AnyRef"))), emptyValDef, List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))))))), Literal(Constant(()))))

    // AST for defining a class with a templated parent -- but showRaw is wrong here!
    scala> ru.showRaw{ru.reify{class B extends collection.mutable.Seq[String]}}
    res7: String = Expr(Block(List(ClassDef(Modifiers(), newTypeName("B"), List(), Template(List(AppliedTypeTree(Ident(scala.collection.mutable.Seq), List(Select(Ident(scala.Predef), newTypeName("String"))))), emptyValDef, List(DefDef(Modifiers(), nme.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), Literal(Constant(())))))))), Literal(Constant(()))))

  6. squito created this gist Oct 22, 2013.
    4 changes: 4 additions & 0 deletions goal_class
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    class B(bb: ByteBuffer) extends A {
    def x = bb.getInt(0)
    def y = bb.getFloat(4)
    }
    4 changes: 4 additions & 0 deletions trait
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    trait A {
    def x: Int
    def y: Float
    }