I came in some trouble with the new Milestone 4:
First some imports:
--------------------------------------------------------------------------
import scala.reflect.runtime.{universe => u, currentMirror => m}
import scala.tools.reflect.ToolBox
--------------------------------------------------------------------------
In my opinion some types have an inconsistent implementation of
toString. Some types return only their short names. `scala.Product` for
example returns "Product" instead of "scala.Product".
--------------------------------------------------------------------------
scala> u.typeOf[scala.Product]
res31: reflect.runtime.universe.Type = Product // wrong?
scala> u.typeOf[java.lang.Object]
res32: reflect.runtime.universe.Type = Object // wrong?
scala> u.typeOf[scala.Function1[_,_]]
res33: reflect.runtime.universe.Type = scala.Function1[_, _] // correct?
--------------------------------------------------------------------------
ToolBox potentially has a bug in the implementation of `parseExpr`. A
call with a string containing a syntax error, produces a ToolBoxError. A
subsequent call with a string containing correct code results in the
same error:
--------------------------------------------------------------------------
scala> val tb = m.mkToolBox()
tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] =
scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@1f521f12
scala> tb.parseExpr("def x = {}") // works fine
res34: tb.u.Tree = def x = ()
scala> tb.parseExpr("def x = {")
scala.tools.reflect.ToolBoxError: reflective compilation has failed:
'}' expected but eof found.
<strack-trace>
scala> tb.parseExpr("def x = {}") // doesn't work any more
scala.tools.reflect.ToolBoxError: reflective compilation has failed:
'}' expected but eof found.
<strack-trace>
--------------------------------------------------------------------------
How can one get a reference to a self reference or better how to get
information that a class is based on a trait?
--------------------------------------------------------------------------
trait T {
def m: Int
}
class A {
self: T => // how to get reference to this? Is it possible?
def a = m*2
}
--------------------------------------------------------------------------
How can one call a method of an instance? The way I did it, an
AssertionError is thrown. What does it mean and how to avoid it?
--------------------------------------------------------------------------
scala> class X { def meth(i: Int) = i*2 }
defined class X
scala> val x = new X
x: X = X@4ba39183
scala> val insMirr = m.reflect(x)
insMirr: reflect.runtime.universe.InstanceMirror =
scala.reflect.runtime.JavaMirrors$JavaMirror$JavaInstanceMirror@155cd305
scala> val methName = u.newTermName("meth")
methName: reflect.runtime.universe.TermName = meth
scala> val methSym = insMirr.symbol.newMethodSymbol(methName)
methSym: reflect.runtime.universe.MethodSymbol = method meth
scala> val methMirr = insMirr.reflectMethod(methSym)
methMirr: reflect.runtime.universe.MethodMirror =
scala.reflect.runtime.JavaMirrors$JavaMirror$JavaMethodMirror@33220792
scala> methMirr(10)
java.lang.AssertionError: assertion failed: meth
at scala.Predef$.assert(Predef.scala:162)
<more stack-trace>
--------------------------------------------------------------------------
How to use implicit conversions with ToolBox.typeCheck?
--------------------------------------------------------------------------
scala> val tb = m.mkToolBox()
tb: scala.tools.reflect.ToolBox[reflect.runtime.universe.type] =
scala.tools.reflect.ToolBoxFactory$ToolBoxImpl@5849e989
scala> val expr = tb.parseExpr("1 to 3 map (_+1)")
expr: tb.u.Tree =
1.to(3).map(((x$1) => x$1.$plus(1)))
scala> tb.typeCheck(expr)
scala.tools.reflect.ToolBoxError: reflective typecheck has failed: value
to is not a member of Int
at
scala.tools.reflect.ToolBoxFactory$ToolBoxImpl$ToolBoxGlobal$$anonfun$typeCheckExpr$1.apply(ToolBoxFactory.scala:145)
<more stack-trac>
--------------------------------------------------------------------------
I found ToolBox.inferImplicit{Value,View} but I didn't find a way to use
them.
Is there a built-in way to get an easier to read representation of the
FlagSets.FlagSet?
--------------------------------------------------------------------------
scala> case class X(i: Int)
defined class X
scala> u.typeOf[X]
res60: reflect.runtime.universe.Type = X
scala> res60.typeSymbol.flags
res62: reflect.runtime.universe.FlagSet = 2048
--------------------------------------------------------------------------
Currently there is only a Long returned. A Set[NameOfFlag] may be more
useful.
Ok, that's it for now.
Cheers, Antoras