1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
object TestExpressions {
def patmatchScrut {
lazy val z1: Option[String] = { println("forced <z1>"); Some("lazy z1") }
val res = z1 match {
case Some(msg) => msg
case None => "failed"
}
print("lazy val in scrutinee: ")
if (res == "lazy z1")
println("ok")
else
println("failed")
}
def patmatchCase {
val t: Option[String] = Some("test")
val res = t match {
case Some(msg) =>
lazy val z1 = { println("forced <z1>"); "lazy z1" }
z1
case None => "failed"
}
print("lazy val in case: ")
if (res == "lazy z1")
println("ok")
else
println("failed")
}
def patmatchPat {
lazy val Z1 = { println("forced <z1>"); "lazy Z1" }
print("lazy val in case: ")
val t: Option[String] = Some("lazy Z1")
t match {
case Some(Z1) =>
println("ok")
case None =>
println("failed")
}
}
def ifcond {
lazy val z1 = { println("forced <z1>"); "lazy z1" }
print("lazy val in if condition: ")
if (z1 == "lazy z1")
println("ok")
else
println("failed")
}
lazy val LazyField = { println("forced LazyField"); "LazyField" }
def testPatMatchField {
print("lazy val in pattern: ")
val t: Option[String] = Some("LazyField")
t match {
case Some(LazyField) =>
println("ok")
case None =>
println("failed")
}
}
lazy val (x, y) = ({print("x"); "x"}, {print("y"); "y"})
def testPatLazyVal {
println("lazy val with patterns:")
print("x and y: ")
println("(" + x + ", " + y + ")")
lazy val (x1, y1) = ({print("x1"); "x1"}, {print("y1"); "y1"})
print("x1 and y1: ")
println("(" + x1 + ", " + y1 + ")")
}
def test {
patmatchScrut
patmatchCase
patmatchPat
ifcond
testPatMatchField
testPatLazyVal
}
}
object Test extends App {
TestExpressions.test
}
|