Skip to content

Commit bc43b3a

Browse files
authored
Merge pull request #7156 from dotty-staging/change-lambda-syntax
Require (...) around parameters of a lambda
2 parents dc88e01 + 8df2abc commit bc43b3a

27 files changed

+66
-56
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,9 +423,19 @@ object Parsers {
423423
/** Convert tree to formal parameter list
424424
*/
425425
def convertToParams(tree: Tree): List[ValDef] = tree match {
426-
case Parens(t) => convertToParam(t) :: Nil
427-
case Tuple(ts) => ts map (convertToParam(_))
428-
case t => convertToParam(t) :: Nil
426+
case Parens(t) =>
427+
convertToParam(t) :: Nil
428+
case Tuple(ts) =>
429+
ts.map(convertToParam(_))
430+
case t: Typed =>
431+
in.errorOrMigrationWarning(
432+
em"parentheses are required around the parameter of a lambda${rewriteNotice("-language:Scala2")}",
433+
t.span)
434+
patch(source, t.span.startPos, "(")
435+
patch(source, t.span.endPos, ")")
436+
convertToParam(t) :: Nil
437+
case t =>
438+
convertToParam(t) :: Nil
429439
}
430440

431441
/** Convert tree to formal parameter
@@ -2915,7 +2925,7 @@ object Parsers {
29152925
else from
29162926
}
29172927

2918-
val handleImport: Tree => Tree = { tree: Tree =>
2928+
val handleImport: Tree => Tree = { (tree: Tree) =>
29192929
if (in.token == USCORE) mkTree(importGiven, tree, wildcardIdent() :: Nil)
29202930
else if (in.token == LBRACE) mkTree(importGiven, tree, inBraces(importSelectors()))
29212931
else tree
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
scala> try { 0 } catch { _: Throwable => 1 }
1+
scala> try { 0 } catch { (_: Throwable) => 1 }
22
val res0: Int = 0

tests/neg/i5592.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ object Test {
1111
}
1212

1313
// these are both fine
14-
val eqReflexive1: (x: Obj) => (EQ[x.type, x.type]) = { x: Obj => implicitly }
15-
val eqReflexive2: Forall[[x] =>> EQ[x, x]] = { x: Obj => implicitly }
14+
val eqReflexive1: (x: Obj) => (EQ[x.type, x.type]) = { (x: Obj) => implicitly }
15+
val eqReflexive2: Forall[[x] =>> EQ[x, x]] = { (x: Obj) => implicitly }
1616

1717
// this compiles
1818
val eqSymmetric1: (x: Obj) => (y: Obj) => EQ[x.type, y.type] => EQ[y.type, x.type] = {
19-
{ x: Obj => { y: Obj => { xEqy: EQ[x.type, y.type] => xEqy.commute } } }
19+
{ (x: Obj) => { (y: Obj) => { (xEqy: EQ[x.type, y.type]) => xEqy.commute } } }
2020
}
2121

2222
val eqSymmetric2: Forall[[x] =>> (y: Obj) => (EQ[x, y.type]) => (EQ[y.type, x])] = {
23-
{ x: Obj => { y: Obj => { xEqy: EQ[x.type, y.type] => xEqy.commute } } } // error
23+
{ (x: Obj) => { (y: Obj) => { (xEqy: EQ[x.type, y.type]) => xEqy.commute } } } // error
2424
}
2525

2626
val eqSymmetric3: Forall[[x] =>> Forall[[y] =>> EQ[x, y] => EQ[y, x]]] = {
27-
{ x: Obj => { y: Obj => { xEqy: EQ[x.type, y.type] => xEqy.commute } } } // error
27+
{ (x: Obj) => { (y: Obj) => { (xEqy: EQ[x.type, y.type]) => xEqy.commute } } } // error
2828
}
2929
}

tests/new/patterns.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ object test {
3333
trait John[A,B] {
3434
def filter(x:Any) = x match {
3535
case (x::xs, _) => "ga"
36-
case _ => {x:String => "foobar"}
36+
case _ => { (x:String) => "foobar"}
3737
}
3838
}

tests/pos/depmet_implicit_norm_ret.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ object Test{
2727
// bug: inferred return type = (Stream[A]) => java.lang.Object with Test.ZipWith[B]{type T = Stream[B]}#T
2828
// this seems incompatible with vvvvvvvvvvvvvvvvvvvvvv -- #3731
2929
def map1[A,B](f : A => B) = ZipWith(f)(SuccZipWith) // this typechecks but fails in -Ycheck:first
30-
val tst1: Stream[Int] = map1[String, Int]{x: String => x.length}.apply(Stream("a"))
30+
val tst1: Stream[Int] = map1[String, Int]{(x: String) => x.length}.apply(Stream("a"))
3131

3232
def map2[A,B](f : A => B) = ZipWith(f) // this finds ZeroZipWith where scalac finds SuccZipWith and fails typechecking in the next line.
33-
val tst2: Stream[Int] = map2{x: String => x.length}.apply(Stream("a"))
33+
val tst2: Stream[Int] = map2{(x: String) => x.length}.apply(Stream("a"))
3434
}

tests/pos/gadt-TypeSafeLambda.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ object TypeSafeLambda {
103103
val interped: (Env) => String =
104104
interp[Term, Exp, Prod, Arr, Env, String] (c, exp)
105105

106-
interped((((), 1), { i: Int => i.toString })) : String // "1"
106+
interped((((), 1), { (i: Int) => i.toString })) : String // "1"
107107
}
108108

109109
}

tests/pos/i94-nada.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ trait Test1 {
2525
case class Left[A,B](x: A) extends Either[A,B] with Monad[A]
2626
case class Right[A,B](x: B) extends Either[A,B] with Monad[B]
2727
def flatMap[X,Y,M[X]<:Monad[X]](m: M[X], f: X => M[Y]): M[Y] = f(m.x)
28-
println(flatMap(Right(1), {x: Int => Right(x)}))
28+
println(flatMap(Right(1), {(x: Int) => Right(x)}))
2929
}
3030
trait Test2 {
3131
trait Monad[X] {
@@ -35,9 +35,9 @@ trait Test2 {
3535
case class Left[A,B](x: A) extends Either[A,B] with Monad[A]
3636
case class Right[A,B](x: B) extends Either[A,B] with Monad[B]
3737
def flatMap[X,Y,M[X]](m: M[X], f: X => M[Y]): M[Y]
38-
println(flatMap(Left(1), {x: Int => Left(x)}))
38+
println(flatMap(Left(1), {(x: Int) => Left(x)}))
3939
}
4040
trait Test3 {
4141
def flatMap[X,Y,M[X]](m: M[X], f: X => M[Y]): M[Y]
42-
println(flatMap(Some(1), {x: Int => Some(x)}))
42+
println(flatMap(Some(1), {(x: Int) => Some(x)}))
4343
}

tests/pos/pat_gilles.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
abstract class Table2 {
22

33

4-
val x: Any => Unit = { zz:Any =>
4+
val x: Any => Unit = { (zz:Any) =>
55
zz match {
66
case Table2.CellUpdated(row, column) =>
77
val foo = Table2.CellUpdated(2,2)

tests/pos/t0438.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Foo {
22
implicit def pair2fun2[A, B, C](f: (A, B) => C): ((A, B)) => C =
3-
{p: (A, B) => f(p._1, p._2) }
3+
{(p: (A, B)) => f(p._1, p._2) }
44

55
def foo(f: ((Int, Int)) => Int) = f
66
def bar(x: Int, y: Int) = x + y

tests/pos/t3672.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
object Test {
2-
def foo(f: Int => Int) = () ; foo { implicit x : Int => x + 1 }
3-
def bar(f: Int => Int) = () ; foo { x : Int => x + 1 }
2+
def foo(f: Int => Int) = () ; foo { implicit (x : Int) => x + 1 }
3+
def bar(f: Int => Int) = () ; foo { (x : Int) => x + 1 }
44
}

tests/pos/tcpoly_typesub.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ trait TypeSub {
55
def castSub[f[+x]](fl : f[l]) : f[u]
66
def castSuper[f[-x]](fu : f[u]) : f[l] = {
77
type c[+y] = f[y] => f[l]
8-
castSub[c]{ fl : f[l] => fl }(fu)
8+
castSub[c]{ (fl : f[l]) => fl }(fu)
99
}
1010
def castValue[t](lt : l with t) : u with t = {
1111
type c[+y] = y with t

tests/run-custom-args/no-useless-forwarders.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ trait B {
99

1010
object Test extends A with B{
1111
def main(args: Array[String]) = {
12-
assert(!this.getClass.getDeclaredMethods.exists{x: java.lang.reflect.Method => x.getName == "foo"},
12+
assert(!this.getClass.getDeclaredMethods.exists{(x: java.lang.reflect.Method) => x.getName == "foo"},
1313
"no forwarder is needed here")
14-
assert(!this.getClass.getDeclaredMethods.exists{x: java.lang.reflect.Method => x.getName == "bar"},
14+
assert(!this.getClass.getDeclaredMethods.exists{(x: java.lang.reflect.Method) => x.getName == "bar"},
1515
"no forwarder is needed here")
1616
}
1717
}

tests/run-staging/staged-streams_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ object Test {
437437
})
438438
case Many => producer.init(st => '{
439439
val oldnadv: Unit => Unit = ${nadv.get}
440-
val adv1: Unit => Unit = { _: Unit => {
440+
val adv1: Unit => Unit = { (_: Unit) => {
441441
if(${producer.hasNext(st)}) {
442442
${producer.step(st, k)}
443443
}

tests/run/Course-2002-02.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ object M4 {
101101
def sumInts = sum(x => x)
102102
def sumCubes = sum(x => x * x * x)
103103
def sumReciprocals = sum(1.0/_)
104-
def sumPi = { n: Int => 4 + sum(x => 4.0/(4*x+1) - 4.0/(4*x-1))(1, n) }
104+
def sumPi = { (n: Int) => 4 + sum(x => 4.0/(4*x+1) - 4.0/(4*x-1))(1, n) }
105105

106106
Console.println(sumInts(1,4))
107107
Console.println(sumCubes(1,4))
@@ -122,7 +122,7 @@ object M5 {
122122
def sumInts = sum(x => x)
123123
def sumCubes = sum(x => x * x * x)
124124
def sumReciprocals = sum(x => 1.0/x)
125-
def sumPi = { n: Int => 4 + sum(x => 4.0/(4*x+1) - 4.0/(4*x-1))(1, n) }
125+
def sumPi = { (n: Int) => 4 + sum(x => 4.0/(4*x+1) - 4.0/(4*x-1))(1, n) }
126126

127127
Console.println(sumInts(1,4))
128128
Console.println(sumCubes(1,4))
@@ -142,7 +142,7 @@ object M6 {
142142
def sumInts = sum(x => x)_
143143
def sumCubes = sum(x => x * x * x)_
144144
def sumReciprocals = sum(x => 1.0/x)_
145-
def sumPi = { n: Int => 4 + sum(x => 4.0/(4*x+1) - 4.0/(4*x-1))(1, n) }
145+
def sumPi = { (n: Int) => 4 + sum(x => 4.0/(4*x+1) - 4.0/(4*x-1))(1, n) }
146146

147147
Console.println(sumInts(1,4))
148148
Console.println(sumCubes(1,4))
@@ -165,7 +165,7 @@ object M7 {
165165
def sumInts = sum(x => x)_
166166
def sumCubes = sum(x => x * x * x)_
167167
def sumReciprocals = sum(x => 1.0/x)_
168-
def sumPi = { n: Int => 4 + sum(x => 4.0/(4*x+1) - 4.0/(4*x-1))(1, n) }
168+
def sumPi = { (n: Int) => 4 + sum(x => 4.0/(4*x+1) - 4.0/(4*x-1))(1, n) }
169169

170170
Console.println(sumInts(1,4))
171171
Console.println(sumCubes(1,4))
@@ -182,7 +182,7 @@ object M8 {
182182
if (a > b) 1
183183
else f(a) * product(f)(a + step, step, b);
184184

185-
def productPi = { n: Int => product(x=>4.0*x*x/(2*x-1)/(2*x-1))(1,1,n)/n }
185+
def productPi = { (n: Int) => product(x=>4.0*x*x/(2*x-1)/(2*x-1))(1,1,n)/n }
186186

187187
val pi = 2 * product(x => x * x)(2, 2, 40) / product(x => x * x)(1, 2,40)/40;
188188

@@ -210,9 +210,9 @@ object M9 {
210210
def sumInts = sum(x => x)
211211
def sumCubes = sum(x => x * x * x)
212212
def sumReciprocals = sum(x => 1.0 / x)
213-
def sumPi = { n: Int => 4 + sum(x => 4.0/(4*x+1) - 4.0/(4*x-1))(1, n) }
213+
def sumPi = { (n: Int) => 4 + sum(x => 4.0/(4*x+1) - 4.0/(4*x-1))(1, n) }
214214

215-
def productPi = { n: Int => product(x=>4.0*x*x/(2*x-1)/(2*x-1))(1,n)/n }
215+
def productPi = { (n: Int) => product(x=>4.0*x*x/(2*x-1)/(2*x-1))(1,n)/n }
216216

217217
val pi = 2*product(x => 2*x*2*x)(1,20)/product(x =>(2*x-1)*(2*x-1))(1,20)/40
218218

tests/run/Course-2002-05.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ object M0 {
2020
xs
2121
else {
2222
val pivot = xs.head;
23-
val sub = partition(xs.tail, { elem : a => less(elem, pivot) });
23+
val sub = partition(xs.tail, { (elem : a) => less(elem, pivot) });
2424
quicksort(less)(sub._1) ::: List(pivot) ::: quicksort(less)(sub._2)
2525
}
2626
}
@@ -96,7 +96,7 @@ object M2 {
9696
else {
9797
val x = s.head;
9898
val withoutX = powerset(s.tail);
99-
withoutX ::: withoutX.map { s1 : List[a] => x::s1 }
99+
withoutX ::: withoutX.map { (s1 : List[a]) => x::s1 }
100100
}
101101
}
102102

tests/run/Course-2002-06.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ object M0 {
155155
* changing the appearance of the painter
156156
*/
157157
def transformPainter(origin: Vector, newX: Vector, newY: Vector)(painter: Painter): Painter = {
158-
frame: Frame => {
158+
(frame: Frame) => {
159159
val newOrigin = frame.coordMap(origin);
160160
val newFrame = new Frame(newOrigin,
161161
frame.coordMap(newX) - newOrigin,
@@ -182,7 +182,7 @@ object M0 {
182182
/** Compose a painter that draws p1 on the left of p2
183183
*/
184184
def beside(p1: Painter, p2: Painter) : Painter = {
185-
frame: Frame => {
185+
(frame: Frame) => {
186186
transformPainter(new Vector(0.0, 0.0),
187187
new Vector(0.5, 0.0),
188188
new Vector(0.0, 1.0))(p1)(frame);
@@ -195,7 +195,7 @@ object M0 {
195195
/** Compose a painter that draws p1 below p2
196196
*/
197197
def below(p1: Painter, p2: Painter): Painter = {
198-
frame: Frame => {
198+
(frame: Frame) => {
199199
transformPainter(new Vector(0.0, 0.0),
200200
new Vector(1.0, 0.0),
201201
new Vector(0.0, 0.5))(p1)(frame);

tests/run/Course-2002-10.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ object M0 {
2424
object M1 {
2525

2626
def scale(x: Double, s: LazyList[Double]): LazyList[Double] =
27-
s map { e: Double => e*x }
27+
s map { (e: Double) => e*x }
2828

2929
def partialSums(s: LazyList[Double]): LazyList[Double] =
3030
LazyList.cons(s.head, partialSums(s.tail) map (x => x + s.head));
@@ -45,14 +45,14 @@ object M1 {
4545
better(s, transform) map (x => x.head);
4646

4747
def lnSummands(n: Double): LazyList[Double] =
48-
LazyList.cons(1.0 / n, lnSummands(n + 1.0) map { x: Double => -x })
48+
LazyList.cons(1.0 / n, lnSummands(n + 1.0) map { (x: Double) => -x })
4949

5050
var ln0 = partialSums(lnSummands(1.0));
5151
var ln1 = euler(ln0);
5252
var ln2 = veryGood(ln0, euler);
5353

5454
def piSummands(n: Double): LazyList[Double] =
55-
LazyList.cons(1.0 / n, piSummands(n + 2.0) map { x: Double => -x })
55+
LazyList.cons(1.0 / n, piSummands(n + 2.0) map { (x: Double) => -x })
5656

5757
var pi0 = scale(4.0, partialSums(piSummands(1.0)));
5858
var pi1 = euler(pi0);

tests/run/implicitFuns.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,17 @@ object TransactionalExpansion {
191191
trans.commit()
192192
}
193193

194-
def thisTransaction = $t: Transaction => $t
194+
def thisTransaction = ($t: Transaction) => $t
195195

196-
def f1(x: Int) = { $t: Transaction =>
196+
def f1(x: Int) = { ($t: Transaction) =>
197197
thisTransaction.apply($t).println(s"first step: $x")
198198
f2(x + 1).apply($t)
199199
}
200-
def f2(x: Int) = { $t: Transaction =>
200+
def f2(x: Int) = { ($t: Transaction) =>
201201
thisTransaction.apply($t).println(s"second step: $x")
202202
f3(x * x).apply($t)
203203
}
204-
def f3(x: Int) = { $t: Transaction =>
204+
def f3(x: Int) = { ($t: Transaction) =>
205205
thisTransaction.apply($t).println(s"third step: $x")
206206
if (x % 2 != 0) thisTransaction.apply($t).abort()
207207
x

tests/run/iterators.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ object Test {
9595
}
9696

9797
def check_findIndexOf: String = {
98-
val i = List(1, 2, 3, 4, 5).indexWhere { x: Int => x >= 4 }
99-
val j = List(1, 2, 3, 4, 5).indexWhere { x: Int => x >= 16 }
98+
val i = List(1, 2, 3, 4, 5).indexWhere { (x: Int) => x >= 4 }
99+
val j = List(1, 2, 3, 4, 5).indexWhere { (x: Int) => x >= 16 }
100100
"" + i + "x" + j
101101
}
102102

tests/run/mapConserve.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ object Test {
2828
}
2929

3030
var callCount = 0
31-
val lastHexDigit: Function1[BigInt, AnyRef] = { x: BigInt => callCount+=1; if (x < 16) x else x % 16 }
31+
val lastHexDigit: Function1[BigInt, AnyRef] = { (x: BigInt) => callCount+=1; if (x < 16) x else x % 16 }
3232

3333
def main(args: Array[String]): Unit = {
3434
for (length <- 0 to maxListLength;
3535
bitmap <- 0 until (1 << length);
36-
data = List.range(0, length) map { x: Int =>
36+
data = List.range(0, length) map { (x: Int) =>
3737
if ((bitmap & (1 << x)) != 0) BigInt(x+16)
3838
else BigInt(x)
3939
})

tests/run/serialization-new.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ object Test1_scala {
105105
println()
106106

107107
// Function
108-
val f1 = { x: Int => 2 * x }
108+
val f1 = { (x: Int) => 2 * x }
109109
val _f1: Function[Int, Int] = read(write(f1))
110110
println("f1 = <na>")
111111
println("_f1 = <na>")

tests/run/t216.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Test extends App {
22
object m {
3-
val f = { x: Unit => () }
3+
val f = { (x: Unit) => () }
44
Console.println("OK")
55
}
66
m;

tests/run/t5629b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object Test extends App {
1111
trait MySmartPF[@specialized(Int) -A] extends MyPF[A] {
1212
def apply(x: A): Unit = {
1313
println("MySmartPF.apply entered...")
14-
applyOrElse(x, { default: Any => throw new MatchError(default) })
14+
applyOrElse(x, { (default: Any) => throw new MatchError(default) })
1515
}
1616
}
1717

tests/run/t5665.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ object O {
22
trait T {
33
private[this] val c: Int = 42
44
def f =
5-
{ x: Int => c }
5+
{ (x: Int) => c }
66
}
77
}
88

tests/run/tuple-ops.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ val r5: Unit = a.zip(d)
1313
// Map
1414
case class Foo[X](x: X)
1515

16-
val r6: (Int, Int, Int) = a.map[[t] =>> Int]([t] => x: t => x match {
16+
val r6: (Int, Int, Int) = a.map[[t] =>> Int]([t] => (x: t) => x match {
1717
case x: Int => x * x
1818
case _ => ???
1919
})
2020

2121
val r7: ((1, Foo[1]), (2, Foo[2]), (3, Foo[3])) =
22-
a.map[[t] =>> (t, Foo[t])]( [t] => x: t => (x, Foo(x)) )
22+
a.map[[t] =>> (t, Foo[t])]( [t] => (x: t) => (x, Foo(x)) )
2323

2424
// More Zip
2525
val t1: Int *: Long *: Tuple = (1, 2l, 100, 200)

0 commit comments

Comments
 (0)