Skip to content

Instantly share code, notes, and snippets.

@pnf
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pnf/fa49cc85f15faae392b2 to your computer and use it in GitHub Desktop.
Save pnf/fa49cc85f15faae392b2 to your computer and use it in GitHub Desktop.
blehblehblehpnf
blehblehblehpnf
scala> classOf[Enumeration#Value].getDeclaredFields.find(_.toString.contains("outerEnum"))
res137: Option[java.lang.reflect.Field] = Some(private final scala.Enumeration scala.Enumeration$Value.scala$Enumeration$$outerEnum)
scala> classOf[Enumeration#Value].getDeclaredFields.find(_.toString.contains("outerEnum")).get
res138: java.lang.reflect.Field = private final scala.Enumeration scala.Enumeration$Value.scala$Enumeration$$outerEnum
scala> val f = classOf[Enumeration#Value].getDeclaredFields.find(_.toString.contains("outerEnum")).get
f: java.lang.reflect.Field = private final scala.Enumeration scala.Enumeration$Value.scala$Enumeration$$outerEnum
scala> f.setAccessible(true)
scala> ev
res140: EEE.EEE = $anon$1@0
scala> f.get(ev)
res141: Object = EEE
scala> reify {classOf[EEE.EEE]}
res142: reflect.runtime.universe.Expr[Class[EEE.EEE]] = Expr[java.lang.Class[EEE.EEE](classOf[scala.Enumeration$Value])](classOf[scala.Enumeration$Value])
scala> showRaw(reify {classOf[EEE.EEE]})
res143: String = Expr(Literal(Constant(EEE.EEE)))
scala> Constant("EEE.EEE")
res144: reflect.runtime.universe.Constant = Constant(EEE.EEE)
scala>
scala> f.get((new EEE.EEE{def id=0})).asInstanceOf[Enumeration].withName("foo")
res165: Enumeration#Value = foo
scala> reify{f.get((new EEE.EEE{def id=0})).asInstanceOf[Enumeration].withName("foo")}
res166: reflect.runtime.universe.Expr[Enumeration#Value] =
Expr[Enumeration#Value]($read.f.get({
final class $anon extends $read.EEE.EEE {
def <init>() = {
super.<init>();
()
};
def id = 0
};
new $anon()
}).asInstanceOf[Enumeration].withName("foo"))
scala> showRaw(reify{f.get((new EEE.EEE{def id=0})).asInstanceOf[Enumeration].withName("foo")})
res167: String = Expr(Apply(Select(TypeApply(Select(Apply(Select(Select(Select(Select(Select(Select(Ident($line231.$read), TermName("$iw")), TermName("$iw")), TermName("$iw")), TermName("$iw")), TermName("f")), TermName("get")), List(Block(List(ClassDef(Modifiers(FINAL), TypeName("$anon"), List(), Template(List(Select(Select(Select(Select(Select(Select(Ident($line153.$read), TermName("$iw")), TermName("$iw")), TermName("$iw")), TermName("$iw")), TermName("EEE")), TypeName("EEE"))), noSelfType, List(DefDef(Modifiers(), termNames.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(typeNames.EMPTY), typeNames.EMPTY), termNames.CONSTRUCTOR), List())), Literal(Constant(())))), DefDef(Modifiers(), TermName("id"), List(), List(), TypeTree(), Literal(Constant(0))))...
scala>
scala> showRaw(reify{(new EEE{def id=0})})
res179: String = Expr(Block(List(ClassDef(Modifiers(FINAL), TypeName("$anon"), List(), Template(List(Select(Select(Select(Select(Select(Ident($line137.$read), TermName("$iw")), TermName("$iw")), TermName("$iw")), TermName("$iw")), TypeName("EEE"))), noSelfType, List(DefDef(Modifiers(), termNames.CONSTRUCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(typeNames.EMPTY), typeNames.EMPTY), termNames.CONSTRUCTOR), List())), Literal(Constant(())))), DefDef(Modifiers(), TermName("id"), List(), List(), TypeTree(), Literal(Constant(0))))))), Apply(Select(New(Ident(TypeName("$anon"))), termNames.CONSTRUCTOR), List())))
scala>
scala> def bqTree(c:Context) = {import c.universe._; val view = c.inferImplicitView(q""" "abc" """,typeOf[String],typeOf[Int]); q""" $view("abcd") """}
warning: there was one deprecation warning; re-run with -deprecation for details
bqTree: (c: reflect.macros.Context)c.universe.Tree
scala> def bqImpl(c:Context) = c.Expr[Int](bqTree(c))
warning: there was one deprecation warning; re-run with -deprecation for details
bqImpl: (c: reflect.macros.Context)c.Expr[Int]
scala> def bqRawImpl(c:Context) = c.literal(c.universe.showRaw(bqTree(c)))
warning: there were two deprecation warnings; re-run with -deprecation for details
bqRawImpl: (c: reflect.macros.Context)c.Expr[String]
scala> def bq = macro bqImpl
warning: there was one deprecation warning; re-run with -deprecation for details
defined term macro bq: Int
scala> def bqRaw = macro bqRawImpl
warning: there was one deprecation warning; re-run with -deprecation for details
defined term macro bqRaw: String
scala> def brTree(c:Context) = {import c.universe._; val view = c.Expr[String=>Int](c.inferImplicitView(c.literal("abc").tree,typeOf[String],typeOf[Int])); (reify {view.splice("abc")}).tree}
warning: there were two deprecation warnings; re-run with -deprecation for details
brTree: (c: reflect.macros.Context)c.universe.Tree
scala> def brImpl(c:Context) = c.Expr[Int](brTree(c))
warning: there was one deprecation warning; re-run with -deprecation for details
brImpl: (c: reflect.macros.Context)c.Expr[Int]
scala> def brRawImpl(c:Context) = c.literal(c.universe.showRaw(brTree(c)))
warning: there were two deprecation warnings; re-run with -deprecation for details
brRawImpl: (c: reflect.macros.Context)c.Expr[String]
scala> def brRaw = macro brRawImpl
warning: there was one deprecation warning; re-run with -deprecation for details
defined term macro brRaw: String
scala> def br = macro brImpl
warning: there was one deprecation warning; re-run with -deprecation for details
defined term macro br: Int
scala> br
<console>:15: error: missing arguments for method s2i;
follow this method with `_' if you want to treat it as a partially applied function
br
^
scala> brRaw
res12: String = Apply(Select(Select(Select(Select(Select(Ident($line16), $line16.$read), $iw), $iw), TermName("s2i")), TermName("apply")), List(Literal(Constant("abc"))))
scala> bq
res13: Int = 4
scala> bqRaw
res14: String = Apply(Select(Select(Select(Select(Ident($line16), $line16.$read), $iw), $iw), TermName("s2i")), List(Literal(Constant("abcd"))))
scala> def infImpl(c:Context) = {import c.universe._; val f = c.inferImplicitView(c.literal("abc").tree,typeOf[String],typeOf[Int]); c.Expr[Int](Apply(f,List(Literal(Constant("abcd")))))}
warning: there were two deprecation warnings; re-run with -deprecation for details
infImpl: (c: reflect.macros.Context)c.Expr[Int]
scala> def inf = macro infImpl
warning: there was one deprecation warning; re-run with -deprecation for details
defined term macro inf: Int
scala> inf
res3: Int = 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment