Skip to content

Commit b8b6a68

Browse files
committed
Allow single-case lambda after colon
1 parent 3ae53ff commit b8b6a68

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,8 @@ object Parsers {
11221122
else if lookahead.token == LPAREN || lookahead.token == LBRACKET then
11231123
lookahead.skipParens()
11241124
isArrowIndent()
1125+
else if lookahead.token == CASE then
1126+
Some(() => singleCaseMatch())
11251127
else
11261128
None
11271129

@@ -2378,7 +2380,7 @@ object Parsers {
23782380

23792381
/** Expr ::= [`implicit'] FunParams (‘=>’ | ‘?=>’) Expr
23802382
* | TypTypeParamClause ‘=>’ Expr
2381-
* | ExprCaseClause
2383+
* | ExprCaseClause -- under experimental.relaxedLambdaSyntax
23822384
* | Expr1
23832385
* FunParams ::= Bindings
23842386
* | id
@@ -2794,6 +2796,7 @@ object Parsers {
27942796
* ColonArgument ::= colon [LambdaStart]
27952797
* indent (CaseClauses | Block) outdent
27962798
* | colon LambdaStart expr ENDlambda -- under experimental.relaxedLambdaSyntax
2799+
* | colon ExprCaseClause -- under experimental.relaxedLambdaSyntax
27972800
* LambdaStart ::= FunParams (‘=>’ | ‘?=>’)
27982801
* | TypTypeParamClause ‘=>’
27992802
* ColonArgBody ::= indent (CaseClauses | Block) outdent

docs/_docs/internals/syntax.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ ColonArgument ::= colon [LambdaStart]
298298
indent (CaseClauses | Block) outdent
299299
| colon LambdaStart expr ENDlambda -- ENDlambda is inserted for each production at next EOL
300300
-- does not apply if enclosed in parens
301+
| colon ExprCaseClause
301302
LambdaStart ::= FunParams (‘=>’ | ‘?=>’)
302303
| TypTypeParamClause ‘=>’
303304
Quoted ::= ‘'’ ‘{’ Block ‘}’

tests/pos/closure-args.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ val d2: String = xs
1313
.filter: z => !z.isEmpty
1414
(0)
1515

16+
val d3: String = xs
17+
.map: x => x.toString + xs.collect: case y if y > 0 => y
18+
.filter: z => !z.isEmpty
19+
(0)
20+

tests/run/single-case-expr.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ case class Foo(x: Int, y: Int)
88
val foo = Foo(1, 2)
99
assert(g(foo) == 1)
1010

11+
val h1: Foo => Int = identity: case Foo(a, b) => a
12+
val h2: Foo => Int = identity: case Foo(a, b) =>
13+
a
14+
1115
val a1 = Seq((1, 2), (3, 4)).collect(case (a, b) if b > 2 => a)
1216
assert(a1 == Seq(3))
1317

@@ -18,4 +22,9 @@ case class Foo(x: Int, y: Int)
1822
)
1923
assert(a2 == Seq(1, 3))
2024

25+
val a3 = Seq((1, 2), (3, 4)).collect: case (a, b) if b > 2 => a
26+
assert(a3 == Seq(3))
27+
2128
val partial: PartialFunction[(Int, Int), Int] = case (a, b) if b > 2 => a
29+
30+

0 commit comments

Comments
 (0)