电梯代码:
import net.liftweb.util.Helpers._
object test extends App {
val xml = <div>
<span name="a">a</span>
<span name="b">b</span>
</div>
val t1 = if (true) {
"@a" #> "xxx"
} else {
"@a" #> "yyy"
} &
"@b" #> "zzz"
val t2 = (if (true) {
"@a" #> "xxx"
} else {
"@a" #> "yyy"
}) &
"@b" #> "zzz"
println("------------- t1 ------------")
println(t1(xml))
println("------------- t2 ------------")
println(t2(xml))
}
输出:
------------- t1 ------------
<div>
xxx
<span name="b">b</span>
</div>
------------- t2 ------------
<div>
xxx
zzz
</div>
奇怪,对吧?唯一的区别是t2
包裹if else
了()
。
我的朋友反编译了字节码,发现scala编译了t1
类似的:
t1 = true ? "@a" #> "xxx" : "@a" #> "yyy" & "@b" #> "zzz"
好像代码是:
if (true)
"@a" #> "xxx"
else
"@a" #> "yyy" &
"@b" #> "zzz"
哪里错了?