Skip to content

Instantly share code, notes, and snippets.

View xeno-by's full-sized avatar

Eugene Burmako xeno-by

View GitHub Profile
import scala.reflect.macros.WhiteboxContext
import scala.language.experimental.macros
object Macros {
def impl[T](c: WhiteboxContext)(implicit T: c.WeakTypeTag[T]) = {
import c.universe._
val allMeths = T.tpe.declarations.collect { case m: MethodSymbol => m }
val goodMeths = allMeths.filter(m => m.isPublic && !m.isConstructor && !m.isSynthetic)
val defDefs = goodMeths.map(m => {
def toMods(sym: Symbol) =
import scala.reflect.macros.{BlackboxContext, WhiteboxContext}
import scala.annotation.compileTimeOnly
import scala.language.experimental.macros
object Macros {
def impl(c: WhiteboxContext)(body: c.Tree) = {
import c.universe._
val Block(stats0, expr) = body
val stats: List[Tree] = stats0 :+ expr
def yyTransform(stats: List[Tree]): List[List[Tree]] = {
import shapeless._
object Test extends App {
implicitly[Generic.Aux[(Int, Char), Int :: Char :: HNil]]
}
[error] ~/Projects/shapeless-test/Test.scala:3: could not find implicit value for parameter e: shapeless.Generic.Aux[(Int, Char),shapeless.::[Int,shapeless.::[Char,shapeless.HNil]]]
[error] implicitly[Generic.Aux[(Int, Char), Int :: Char :: HNil]]
[error] ^
23:03 ~/Projects/sbt-example-paradise (master)$ sbt
[info] Loading project definition from /Users/xeno_by/Projects/sbt-example-paradise/project
[info] Updating {file:/Users/xeno_by/Projects/sbt-example-paradise/project/}sbt-example-paradise-build...
[info] Resolving org.fusesource.jansi#jansi;1.4 ...
[info] downloading http://repo1.maven.org/maven2/net/virtual-void/sbt-dependency-graph_2.10_0.13/0.7.4/sbt-dependency-graph-0.7.4.jar ...
[info] [SUCCESSFUL ] net.virtual-void#sbt-dependency-graph;0.7.4!sbt-dependency-graph.jar (986ms)
[info] downloading http://repo1.maven.org/maven2/com/github/mdr/ascii-graphs_2.10/0.0.3/ascii-graphs_2.10-0.0.3.jar ...
[info] [SUCCESSFUL ] com.github.mdr#ascii-graphs_2.10;0.0.3!ascii-graphs_2.10.jar (780ms)
[info] downloading http://repo1.maven.org/maven2/org/scala-lang/scala-library/2.10.1/scala-library-2.10.1.jar ...
[info] [SUCCESSFUL ] org.scala-lang#scala-library;2.10.1!scala-library.jar (2760ms)
import scala.reflect.macros.Context
import scala.language.experimental.macros
import scala.reflect.api.Liftable
object liftableMacro {
implicit def liftableCaseClass[T]: Liftable[T] = macro impl
def impl(c: Context): c.Tree = {
import c.universe._
val spliced: List[Tree] = List(q"???")
val s = ""
[error] /Users/xeno_by/Projects/macros-playground/macros/src/main/scala/Macros.scala:50: Can't splice String(""), consider
[error] Apply(Select(New(Ident(TermName(${""}))),
[error] ^
// As I mentioned before, macros can do everything that the compiler can do by judicious application of casts.
// Here we get a reference to a typechecker servicing the expansion and from it - to the list of lexical scopes.
// Having that information at hand, we can report the list of variables in both lexical and "this" scopes.
// Since casts are involved here, there's no guarantee that this will work even in-between minor releases of Scala.
// Therefore I would advise going this route only if there are no workarounds for your particular use case.
// Please feel free to email me to ask for workarounds (but please don't leave comments here, because I don't get notified about them)
import scala.reflect.macros.Context
import scala.language.experimental.macros
private[this] val r1: reflect.runtime.universe.Tree = {
import scala.reflect.api.Liftable;
{
final class $anon extends AnyRef with scala.reflect.api.Liftable[A] {
def <init>(): <$anon: scala.reflect.api.Liftable[A]> = {
$anon.super.<init>();
()
};
def apply(universe: scala.reflect.api.Universe, cc: A): universe.Tree = {
val ttree: universe.TypeTree = universe.TypeTree(universe.typeOf[A]({
case class A()
object Test extends App {
import Macros._
import scala.reflect.runtime.universe._
val c1 = A()
val r1 = q"$c1"
}
import scala.reflect.macros.WhiteboxContext
import scala.language.experimental.macros
import scala.reflect.api.Liftable
import scala.reflect.macros.Universe
import scala.reflect.api.Position
object Macros {
implicit def liftableCaseClass[T]: Liftable[T] = macro impl[T]
def impl[T: c.WeakTypeTag](c: WhiteboxContext) = {
import c.universe._