Created
April 2, 2014 05:24
-
-
Save dstu/9928397 to your computer and use it in GitHub Desktop.
Failure to find type class implementation with Scala 2.10.4, Macro Paradise 2.0.0-M6
This file contains hidden or 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 mymacro.test | |
import scala.annotation.StaticAnnotation | |
import scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
trait MyTypeClass[T] { | |
def something: Int | |
} | |
class MyMacro extends StaticAnnotation { | |
def macroTransform(annottees: Any*) = macro MyMacro.impl | |
} | |
object MyMacro { | |
private[this] def buildDeclarations(c: Context)(classDeclaration: c.universe.ClassDef, | |
companionDeclaration: Option[c.universe.ModuleDef]): c.Tree = { | |
import c.universe._ | |
val q"$modifiers class $typeName[..$tparams] $constructorModifiers(..$first_params)(...$rest_params) extends $early with ..$parents { ..$body }" = classDeclaration | |
q""" | |
class $typeName { | |
def stuff = println("hi") | |
} | |
object ${typeName.toTermName} { | |
implicit object isTypeClass extends _root_.mymacro.test.MyTypeClass[$typeName] { | |
override def something = 5 | |
} | |
} | |
""" | |
} | |
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { | |
import c.universe._ | |
val inputs = annottees.map(_.tree).toList | |
val declarations = inputs match { | |
case Nil => | |
c.abort(c.enclosingPosition, "No struct target") | |
case (classDeclaration: ClassDef) :: Nil => | |
buildDeclarations(c)(classDeclaration, None) | |
case (classDeclaration: ClassDef) :: (companionDeclaration: ModuleDef) :: Nil => | |
buildDeclarations(c)(classDeclaration, Some(companionDeclaration)) | |
case _ => | |
c.abort(c.enclosingPosition, "Invalid struct target") | |
} | |
println(declarations) | |
c.Expr(declarations) | |
} | |
} | |
@MyMacro class Thingamajig | |
class ManualThingamajig { | |
val asdf = 1.0f | |
} | |
object ManualThingamajig { | |
implicit object isTypeClass extends MyTypeClass[ManualThingamajig] { | |
override def something = -2 | |
} | |
} | |
object TestApp extends App { | |
implicitly[MyTypeClass[ManualThingamajig]] | |
implicitly[MyTypeClass[Thingamajig]] // unable to resolve | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
scalamacros/paradise#13