aboutsummaryrefslogtreecommitdiff
path: root/tests/pos/Fileish.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/Fileish.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/Fileish.scala')
-rw-r--r--tests/pos/Fileish.scala53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/pos/Fileish.scala b/tests/pos/Fileish.scala
new file mode 100644
index 000000000..d226bb0bd
--- /dev/null
+++ b/tests/pos/Fileish.scala
@@ -0,0 +1,53 @@
+// Inspired by the original Fileish,
+// testing combinations of lazy and non-lazy vals for their treatment in constructors
+package dotty.tools
+package io
+
+import java.io.{ InputStream }
+import java.util.jar.JarEntry
+import language.postfixOps
+
+/** A common interface for File-based things and Stream-based things.
+ * (In particular, io.File and JarEntry.)
+ */
+class Fileish(val path: Path, val input: () => InputStream) extends Streamable.Chars {
+ def inputStream() = input()
+
+ def parent = path.parent
+ def name = path.name
+ def isSourceFile = path.hasExtension("java", "scala")
+
+ private lazy val pkgLines = lines() collect { case x if x startsWith "package " => x stripPrefix "package" trim }
+ lazy val pkgFromPath = parent.path.replaceAll("""[/\\]""", ".")
+ lazy val pkgFromSource = pkgLines map (_ stripSuffix ";") mkString "."
+
+ override def toString = path.path
+}
+class Fileish2(val path: Path, val input: () => InputStream) extends Streamable.Chars {
+ def inputStream() = input()
+
+ def parent = path.parent
+ def name = path.name
+ def isSourceFile = path.hasExtension("java", "scala")
+
+ private val pkgLines = lines() collect { case x if x startsWith "package " => x stripPrefix "package" trim }
+ lazy val pkgFromPath = parent.path.replaceAll("""[/\\]""", ".")
+ lazy val pkgFromSource = pkgLines map (_ stripSuffix ";") mkString "."
+
+ override def toString = path.path
+}
+
+class Fileish3(val path: Path, val input: () => InputStream) extends Streamable.Chars {
+ def inputStream() = input()
+
+ def parent = path.parent
+ def name = path.name
+ def isSourceFile = path.hasExtension("java", "scala")
+
+ private val pkgLines = lines() collect { case x if x startsWith "package " => x stripPrefix "package" trim }
+ private val pkgFromPath = parent.path.replaceAll("""[/\\]""", ".")
+ private val pkgFromSource = pkgLines map (_ stripSuffix ";") mkString "."
+
+ override def toString = path.path
+}
+