Skip to content

Commit 44095fb

Browse files
committed
More explicit test of trait parameter initialization order
As suggested by @retronym
1 parent 28fff8f commit 44095fb

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/run/traitParamInit.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
object Trace {
2+
private var results = List[Any]()
3+
def apply[A](a: A) = {results ::= a; a}
4+
def fetchAndClear(): Seq[Any] = try results.reverse finally results = Nil
5+
}
6+
trait T(a: Any) {
7+
val ta = a
8+
Trace(s"T.<init>($ta)")
9+
val t_val = Trace("T.val")
10+
}
11+
12+
trait U(a: Any) extends T {
13+
val ua = a
14+
Trace(s"U.<init>($ua)")
15+
}
16+
17+
object Test {
18+
def check(expected: Any) = {
19+
val actual = Trace.fetchAndClear()
20+
if (actual != expected)
21+
sys.error(s"\n$actual\n$expected")
22+
}
23+
def main(args: Array[String]): Unit = {
24+
new T(Trace("ta")) with U(Trace("ua")) {}
25+
check(List("ta", "T.<init>(ta)", "T.val", "ua", "U.<init>(ua)"))
26+
27+
new U(Trace("ua")) with T(Trace("ta")) {}
28+
check(List("ta", "T.<init>(ta)", "T.val", "ua", "U.<init>(ua)"))
29+
}
30+
}

0 commit comments

Comments
 (0)