aboutsummaryrefslogtreecommitdiff
path: root/tests/pos/constrs.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2014-09-24 14:05:43 +0200
committerDmitry Petrashko <dmitry.petrashko@gmail.com>2014-10-11 08:24:37 +0200
commit103d16793f312e2cefc8095de58255728ceebc88 (patch)
treeaf8d3366baa0df30846ca83968172357110dff79 /tests/pos/constrs.scala
parent06173800c03a06b35b3ade30ce52f2dd295851b4 (diff)
downloaddotty-103d16793f312e2cefc8095de58255728ceebc88.tar.gz
dotty-103d16793f312e2cefc8095de58255728ceebc88.tar.bz2
dotty-103d16793f312e2cefc8095de58255728ceebc88.zip
Move private fields into constructor
Private fields that are accessed only from the constructor, and are accessed only after they are properly initialized are now moved into the constructor. This avoids creating a redundant objetc field. Good example: gcd in Rationals (see constrs.scala).
Diffstat (limited to 'tests/pos/constrs.scala')
-rw-r--r--tests/pos/constrs.scala33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/pos/constrs.scala b/tests/pos/constrs.scala
new file mode 100644
index 000000000..dc0e1a369
--- /dev/null
+++ b/tests/pos/constrs.scala
@@ -0,0 +1,33 @@
+class Foo(x: Int, var y: Int) {
+
+ val z: Int = 0
+
+ var u: Int = _
+
+ def f = x
+
+}
+
+class Baz(val base: Int) {
+
+}
+
+
+class Bar(base: Int, byName: => String, local: Int) extends Baz(base + local) {
+
+ def f() = println(base.toString + byName)
+
+}
+
+class Rational(n: Int, d: Int) {
+ def gcd(x: Int, y: Int): Int = ???
+ private val x = gcd(n, d)
+ def numer = n / x
+ def denom = d / x
+}
+class Rational2(n: Int, d: Int) {
+ def gcd(x: Int, y: Int): Int = ???
+ private val x = gcd(n, d)
+ val numer = n / x
+ val denom = d / x
+}