blob: 307915bcac0c643229d084bd0ca18fd55a27c3eb (
plain) (
blame)
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
|
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")
}
}
def test {
patmatchScrut
patmatchCase
patmatchPat
ifcond
testPatMatchField
}
}
object Test extends Application {
TestExpressions.test
}
|