Skip to content

Commit

Permalink
Untuple using def not val.
Browse files Browse the repository at this point in the history
As retronym noted on #897, `val` forces to early.
  • Loading branch information
odersky committed Feb 16, 2016
1 parent 06bfbd3 commit 1729676
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/dotty/tools/dotc/ast/Desugar.scala
Expand Up @@ -591,18 +591,19 @@ object desugar {
/** Map n-ary function `(p1, ..., pn) => body` where n != 1 to unary function as follows:
*
* x$1 => {
* val p1 = x$1._1
* def p1 = x$1._1
* ...
* val pn = x$1._n
* def pn = x$1._n
* body
* }
*/
def makeUnaryCaseLambda(params: List[ValDef], body: Tree)(implicit ctx: Context): Tree = {
def makeTupledFunction(params: List[ValDef], body: Tree)(implicit ctx: Context): Tree = {
val param = makeSyntheticParameter()
def selector(n: Int) = Select(refOfDef(param), nme.selectorName(n))
val vdefs =
params.zipWithIndex.map{
case(param, idx) => cpy.ValDef(param)(rhs = selector(idx))
case (param, idx) =>
DefDef(param.name, Nil, Nil, TypeTree(), selector(idx)).withPos(param.pos)
}
Function(param :: Nil, Block(vdefs, body))
}
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/typer/Typer.scala
Expand Up @@ -624,7 +624,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit

val desugared =
if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head)) {
desugar.makeUnaryCaseLambda(params, fnBody)
desugar.makeTupledFunction(params, fnBody)
}
else {
val inferredParams: List[untpd.ValDef] =
Expand Down
8 changes: 8 additions & 0 deletions tests/run/function-arity.scala
@@ -0,0 +1,8 @@
object Test {
class T[A] { def foo(f: (=> A) => Int) = f(???) }

def main(args: Array[String]): Unit = {
new T[(Int, Int)].foo((ii) => 0)
new T[(Int, Int)].foo((x, y) => 0) // check that this does not run into ???
}
}

0 comments on commit 1729676

Please sign in to comment.