File: nullable-lazyvals.scala

package info (click to toggle)
scala 2.11.12-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 62,828 kB
  • sloc: javascript: 28,808; java: 13,415; xml: 3,250; sh: 1,620; python: 756; makefile: 38; awk: 36; ansic: 6
file content (36 lines) | stat: -rw-r--r-- 976 bytes parent folder | download | duplicates (4)
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

/** Test that call-by-name parameters are set to null if
 *  they are used only to initialize a lazy value, after the
 *  value has been initialized.
 */

class Foo(param1: => Object, param2: => String) {
	lazy val field1 = param1
	lazy val field2 = try param2 finally println("")
}

object Test extends App {
	val foo = new Foo(new Object, "abc")

	foo.field1
	foo.field2

	for (f <- foo.getClass.getDeclaredFields) {
		f.setAccessible(true)
		if (f.getName.startsWith("param")) {
			println("%s: %s".format(f.getName, f.get(foo)))
		}
	}

	// test that try-finally does not generated a liftedTry
	// helper. This would already fail the first part of the test,
	// but this check will help diagnose it (if the single access to a
	// private field does not happen directly in the lazy val, it won't
	// be nulled).
	for (f <- foo.getClass.getDeclaredMethods) {
		f.setAccessible(true)
		if (f.getName.startsWith("lifted")) {
			println("not expected: %s".format(f))
		}
	}
}