From 250418e830bc7ccacf13cb0d3a9121238d99632a Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 30 Oct 2014 17:09:34 +0100 Subject: New phase: PrivateToStatic Make private methods in traits static, so that we do not need to give a default for them. --- tests/pos/CoderTrait.scala | 63 ++++++++++++++++++++++++++++++++++++++++++++++ tests/pos/privates.scala | 9 +++++++ tests/pos/traits.scala | 25 ++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 tests/pos/CoderTrait.scala create mode 100644 tests/pos/privates.scala create mode 100644 tests/pos/traits.scala (limited to 'tests') diff --git a/tests/pos/CoderTrait.scala b/tests/pos/CoderTrait.scala new file mode 100644 index 000000000..1eba60097 --- /dev/null +++ b/tests/pos/CoderTrait.scala @@ -0,0 +1,63 @@ +import collection.mutable.HashMap + +trait CoderTrait { + + val words: List[String] + + (2 -> "ABC", new ArrowAssoc('3') -> "DEF") + + private val mnemonics = Map( + '2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL", + '6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ") + + + ('1', "1") match { + case (digit, str) => true + case _ => false + } + + /** Invert the mnemonics map to give a map from chars 'A' ... 'Z' to '2' ... '9' */ + private val charCode0: Map[Char, Char] = mnemonics withFilter { + case (digit, str) => true + case _ => false + } flatMap { x$1 => + x$1 match { + case (digit, str) => str map (ltr => ltr -> digit) + } + } + + private val charCode: Map[Char, Char] = + for ((digit, str) <- mnemonics; ltr <- str) yield ltr -> digit + + /** Maps a word to the digit string it can represent */ + private def wordCode(word: String): String = word map charCode + + /** A map from digit strings to the words that represent them */ + private val wordsForNum: Map[String, List[String]] = + words groupBy wordCode withDefaultValue Nil + + /** All ways to encode a number as a list of words */ + def encode(number: String): Set[List[String]] = + if (number.isEmpty) Set(Nil) + else { + for { + splitPoint <- 1 to number.length + word <- wordsForNum(number take splitPoint) + rest <- encode(number drop splitPoint) + } yield word :: rest + }.toSet + + /** Maps a number to a list of all word phrases that can represent it */ + def translate(number: String): Set[String] = encode(number) map (_ mkString " ") + +} + +object Coder { + def main(args : Array[String]) : Unit = { + val coder = new CoderTrait { + val words = List("Scala", "sobls", "Python", "Ruby", "C", "A", "rocks", "sucks", "works", "Racka") + } +// println(coder.wordsForNum) + println(coder.translate("7225276257")) + } +} diff --git a/tests/pos/privates.scala b/tests/pos/privates.scala new file mode 100644 index 000000000..edaa10cb6 --- /dev/null +++ b/tests/pos/privates.scala @@ -0,0 +1,9 @@ +trait Test { + + private val x = 2 + + private def foo() = x * x + + private def bar() = foo() + +} diff --git a/tests/pos/traits.scala b/tests/pos/traits.scala new file mode 100644 index 000000000..15310d5a4 --- /dev/null +++ b/tests/pos/traits.scala @@ -0,0 +1,25 @@ +trait B { + + val z: Int + +} + +trait T { + + var x = 2 + + private var xp = 2 + + val y = 3 + + private val yp = 3 + + val z = 4 + + x = 4 + + xp = 4 + +} + +class C extends T -- cgit v1.2.3