Why does Scala reify not work as per the docs?

Scala API docs for 2.10.3 say I can: "Use refiy to create an abstract syntax tree representing this Scala expression." Accordingly, I can do:

scala> val uni = scala.reflect.runtime.universe uni: scala.reflect.api.JavaUniverse = scala.reflect.runtime.JavaUniverse@4e42766 scala> uni reify { 1 to 3 } res2: uni.Expr[scala.collection.immutable.Range.Inclusive] = Expr[scala.collection.immutable.Range.Inclusive](Predef.intWrapper(1).to(3)) 

In the above example, I get what I am looking for: Predef.intWrapper(1).to(3)) (a convenient extended representation of the operations that need to be performed).

When I try to confirm 1 + 3 , however, I cannot imagine the execution of the operations that I desire.

 scala> uni reify { 1 + 3 } res5: uni.Expr[Int(4)] = Expr[Int(4)](4) 

Is this the expected behavior? Is + primitive operation and therefore not reified?

Scala docs show an example of reification, which assumes an even more useful view is available:

 reify{ 2 + 4 } // Apply( Select( Literal(Constant(2)), newTermName("$plus")), List( Literal(Constant(4)) ) ) 

How can I check the correct extended view for 1 + 3 (if it exists) and how can I get a detailed view of any expression (immediately above)?

Edit: Now I see that the view in the Scala showRaw is being created by showRaw . However, I still cannot reproduce the raw view in the example above for 2 + 4 .

+1
source share
1 answer

See this answer by Eugene Burmako:

Macros

are connected to type checking (in the sense that macro arguments are checked by typecheck before expanding the macro), and typechecking folds Constants

reify is implemented using a macro.

You should simply prohibit permanent folding as follows:

 { val i = 1 showRaw{ reify{i + 2}.tree } } // Apply(Select(Ident(newTermName("i")), newTermName("$plus")), List(Literal(Constant(2)))) 
+5
source

All Articles