aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2013-11-04 11:30:14 +0100
committerMartin Odersky <odersky@gmail.com>2013-11-04 11:32:12 +0100
commitbfc7c25bda04697463a36b9eb278e00407895a53 (patch)
tree92f8bc49bd59bf98d604fb29b8d98c04588173e7
parentab95d83444c6397f8859713dd6606602c77c8d23 (diff)
downloaddotty-bfc7c25bda04697463a36b9eb278e00407895a53.tar.gz
dotty-bfc7c25bda04697463a36b9eb278e00407895a53.tar.bz2
dotty-bfc7c25bda04697463a36b9eb278e00407895a53.zip
Fallback to old treatement of tuples.
For interoperability we will keep for the time being the traditional treatment of tuples as instances of Tuple 2… Tuple 22. The new hlist-like treatment will be done in a future step. The flip is controlled by variable "unboxedPairs".
-rw-r--r--src/dotty/tools/dotc/ast/Desugar.scala27
-rw-r--r--src/dotty/tools/dotc/core/Definitions.scala2
-rw-r--r--tests/pos/Coder.scala41
3 files changed, 64 insertions, 6 deletions
diff --git a/src/dotty/tools/dotc/ast/Desugar.scala b/src/dotty/tools/dotc/ast/Desugar.scala
index fb80509fd..d3ffa0c91 100644
--- a/src/dotty/tools/dotc/ast/Desugar.scala
+++ b/src/dotty/tools/dotc/ast/Desugar.scala
@@ -13,6 +13,9 @@ import typer.Mode
object desugar {
+ /** Are we using the new unboxed pair scheme? */
+ private final val unboxedPairs = false
+
import untpd._
private type VarInfo = (NameTree, Tree)
@@ -508,11 +511,25 @@ object desugar {
case Parens(t) =>
t
case Tuple(ts) =>
- def PairTypeTree(l: Tree, r: Tree) =
- AppliedTypeTree(ref(defn.PairClass.typeConstructor), l :: r :: Nil)
- if (ctx.mode is Mode.Type) ts.reduceRight(PairTypeTree)
- else if (ts.isEmpty) unitLiteral
- else ts.reduceRight(Pair(_, _))
+ if (unboxedPairs) {
+ def PairTypeTree(l: Tree, r: Tree) =
+ AppliedTypeTree(ref(defn.PairClass.typeConstructor), l :: r :: Nil)
+ if (ctx.mode is Mode.Type) ts.reduceRight(PairTypeTree)
+ else if (ts.isEmpty) unitLiteral
+ else ts.reduceRight(Pair(_, _))
+ }
+ else {
+ val arity = ts.length
+ def tupleClass = defn.TupleClass(arity)
+ if (arity > Definitions.MaxTupleArity) {
+ ctx.error(s"tuple too long (max allowed: ${Definitions.MaxTupleArity})", tree.pos)
+ unitLiteral
+ }
+ else if (arity == 1) ts.head
+ else if (ctx.mode is Mode.Type) AppliedTypeTree(ref(tupleClass.typeConstructor), ts)
+ else if (arity == 0) unitLiteral
+ else Apply(ref(tupleClass.companionModule.symRef), ts)
+ }
case WhileDo(cond, body) =>
// { <label> def while$(): Unit = if (cond) { body; while$() } ; while$() }
val call = Apply(Ident(nme.WHILE_PREFIX), Nil)
diff --git a/src/dotty/tools/dotc/core/Definitions.scala b/src/dotty/tools/dotc/core/Definitions.scala
index 8731193f6..96fe172b3 100644
--- a/src/dotty/tools/dotc/core/Definitions.scala
+++ b/src/dotty/tools/dotc/core/Definitions.scala
@@ -197,7 +197,7 @@ class Definitions(implicit ctx: Context) {
lazy val StringAdd_+ = StringAddClass.requiredMethod(nme.raw.PLUS)
- lazy val PairClass = requiredClass("dotty.Tuple2")
+ lazy val PairClass = requiredClass("dotty.Pair")
lazy val PartialFunctionClass = requiredClass("scala.PartialFunction")
lazy val AbstractPartialFunctionClass = requiredClass("scala.runtime.AbstractPartialFunction")
lazy val SymbolClass = requiredClass("scala.Symbol")
diff --git a/tests/pos/Coder.scala b/tests/pos/Coder.scala
new file mode 100644
index 000000000..62ceebe80
--- /dev/null
+++ b/tests/pos/Coder.scala
@@ -0,0 +1,41 @@
+import collection.mutable.HashMap
+
+class Coder(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")
+
+ /** Invert the mnemonics map to give a map from chars 'A' ... 'Z' to '2' ... '9' */
+ private val charCode: Map[Char, Char] = mnemonics flatMap { ds =>
+ val digit = ds._1
+ val str = ds._2
+ str map (ltr => ltr -> digit)
+ }
+
+// 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 = ???
+
+ /** A map from digit strings to the words that represent them */
+ private val wordsForNum: Map[String, List[String]] = ???
+
+ /** All ways to encode a number as a list of words */
+ def encode(number: String): Set[List[String]] = ???
+
+ /** Maps a number to a list of all word phrases that can represent it */
+ def translate(number: String): Set[String] = encode(number) map (_ mkString " ")
+
+}
+/*
+/** Test code */
+object Coder {
+ def main(args : Array[String]) : Unit = {
+ val coder = new Coder(List("Scala", "sobls", "Python", "Ruby", "C", "A", "rocks", "sucks", "works", "Racka"))
+// println(coder.wordsForNum)
+ println(coder.translate("7225276257"))
+ }
+}*/