Created
January 25, 2018 19:38
-
-
Save fehu/43d1c1adcab3abbe0b2cb27eeee09d72 to your computer and use it in GitHub Desktop.
Macro Question
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
import scala.language.experimental.macros | |
import scala.reflect.macros.whitebox | |
object MacroQuestion { | |
def apply[F[_], A]: Any = macro impl[F, A] | |
def impl[F[_], A](c: whitebox.Context)( | |
implicit F: c.WeakTypeTag[F[_]], A: c.WeakTypeTag[A] | |
): c.Tree = { | |
import c.universe._ | |
// I want to construct `F[A]`, given types `F[_]` and `A` | |
val tpe = tq"$F[$A]" | |
c.info(c.enclosingPosition, show(tpe), force = true) | |
c.info(c.enclosingPosition, showRaw(tpe), force = true) | |
c.typecheck(tpe) | |
q"()" | |
} | |
} | |
/* | |
scala> MacroQuestion[List, Int] | |
<console>:23: List[Int] | |
MacroQuestion[List, Int] | |
^ | |
<console>:23: AppliedTypeTree(TypeTree(), List(TypeTree())) | |
MacroQuestion[List, Int] | |
^ | |
<console>:23: error: exception during macro expansion: | |
scala.reflect.macros.TypecheckException: Any does not take type parameters | |
at scala.reflect.macros.contexts.Typers$$anonfun$typecheck$2$$anonfun$apply$1.apply(Typers.scala:34) | |
at scala.reflect.macros.contexts.Typers$$anonfun$typecheck$2$$anonfun$apply$1.apply(Typers.scala:28) | |
at scala.reflect.macros.contexts.Typers$$anonfun$3.apply(Typers.scala:24) | |
at scala.reflect.macros.contexts.Typers$$anonfun$3.apply(Typers.scala:24) | |
at scala.reflect.macros.contexts.Typers$$anonfun$withContext$1$1.apply(Typers.scala:25) | |
at scala.reflect.macros.contexts.Typers$$anonfun$withContext$1$1.apply(Typers.scala:25) | |
at scala.reflect.macros.contexts.Typers$$anonfun$1.apply(Typers.scala:23) | |
at scala.reflect.macros.contexts.Typers$$anonfun$1.apply(Typers.scala:23) | |
at scala.reflect.macros.contexts.Typers$class.withContext$1(Typers.scala:25) | |
at scala.reflect.macros.contexts.Typers$$anonfun$typecheck$2.apply(Typers.scala:28) | |
at scala.reflect.macros.contexts.Typers$$anonfun$typecheck$2.apply(Typers.scala:28) | |
at scala.reflect.internal.Trees$class.wrappingIntoTerm(Trees.scala:1716) | |
at scala.reflect.internal.SymbolTable.wrappingIntoTerm(SymbolTable.scala:16) | |
at scala.reflect.macros.contexts.Typers$class.withWrapping$1(Typers.scala:26) | |
at scala.reflect.macros.contexts.Typers$class.typecheck(Typers.scala:28) | |
at scala.reflect.macros.contexts.Context.typecheck(Context.scala:6) | |
at scala.reflect.macros.contexts.Context.typecheck(Context.scala:6) | |
at MacroQuestion$.impl(<console>:35) | |
MacroQuestion[List, Int] | |
^ | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment