scala - Why shapeless `everywhere` combinator works inside a method but not in a `val`? -
given code:
import shapeless._ import poly._ object everywherevalanddef { sealed trait tree[t] case class leaf[t](t: t) extends tree[t] case class node[t](left: tree[t], right: tree[t]) extends tree[t] val tree: tree[int] = node( node( node( leaf(1), node( leaf(2), leaf(3))), leaf(4)), node( leaf(5), leaf(6))) object inc extends ->((i: int) => + 1) def afterincdef = everywhere(inc)(tree) // works val afterincval = everywhere(inc)(tree) // fails val expected: tree[int] = node( node( node( leaf(2), node( leaf(3), leaf(4))), leaf(5)), node( leaf(6), leaf(7))) def eqdef = expected == afterincdef def eqval = expected == afterincval }
in repl session results:
welcome scala version 2.11.4 (java hotspot(tm) 64-bit server vm, java 1.8.0_25). type in expressions have them evaluated. type :help more information. scala> everywherevalanddef.eqdef res0: boolean = true scala> everywherevalanddef.eqval res1: boolean = false scala> everywherevalanddef.tree res2: everywherevalanddef.tree[int] = node(node(node(leaf(1),node(leaf(2),leaf(3))),leaf(4)),node(leaf(5),leaf(6))) scala> everywherevalanddef.afterincdef res3: everywherevalanddef.tree[int] = node(node(node(leaf(2),node(leaf(3),leaf(4))),leaf(5)),node(leaf(6),leaf(7))) scala> everywherevalanddef.afterincval res4: everywherevalanddef.tree[int] = node(node(node(leaf(1),node(leaf(2),leaf(3))),leaf(4)),node(leaf(5),leaf(6)))
why everywhere
not working in val
case?
if move definitions of tree
, leaf
, node
out of everywherevalanddef
top level (or other object) problem goes away.
i don't have definitive answer, appear subtle initialization order issue colliding scala macros issue.
Comments
Post a Comment