Created
May 5, 2015 11:12
-
-
Save retronym/639080041e3fecf58ba9 to your computer and use it in GitHub Desktop.
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
scala> :paste | |
// Entering paste mode (ctrl-D to finish) | |
def impl[T: c.WeakTypeTag](c: Context): c.Tree = { | |
import c.universe._ | |
def subs = weakTypeOf[T].typeSymbol.asClass.knownDirectSubclasses | |
val subs1 = subs | |
println(subs1) | |
val global = c.universe.asInstanceOf[tools.nsc.Global] | |
def checkSubsPostTyper = if (subs1 != subs) | |
c.error(c.macroApplication.pos, "sealed descendents appears after macro exansion") | |
val dummyTypTree = | |
new global.TypeTreeWithDeferredRefCheck()(() => { checkSubsPostTyper ; global.TypeTree(global.NoType) }).asInstanceOf[TypTree] | |
q"type T = $dummyTypTree; ${subs1.size} : _root_.scala.Int" | |
} | |
def numChildren[T]: Int = macro impl[T] | |
// Exiting paste mode, now interpreting. | |
impl: [T](c: scala.reflect.macros.blackbox.Context)(implicit evidence$1: c.WeakTypeTag[T])c.Tree | |
defined term macro numChildren: [T]=> Int | |
scala> numChildren[List[_]] | |
Set(class ::, object Nil) | |
res14: Int = 2 | |
scala> object Test {sealed abstract class C; val x = numChildren[C]; class E extends C } | |
Set() | |
<console>:13: error: sealed descendents appears after macro exansion | |
object Test {sealed abstract class C; val x = numChildren[C]; class E extends C } | |
^ | |
scala> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment