Skip to content

Commit

Permalink
Update patterns.md to disallow chained case expressions
Browse files Browse the repository at this point in the history
This will diagnose a common typo of omitting a comma in a switch expression.
  • Loading branch information
gafter committed Nov 12, 2015
1 parent 49615c9 commit 9ee1b98
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/features/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ relational-expression
;

case-expression
: relational-expression 'case' pattern ':' shift-expression
: shift-expression 'case' pattern ':' shift-expression

This comment has been minimized.

Copy link
@alrz

alrz Nov 20, 2015

Member

What is the use case of this being pattern? Shouldn't it be complex-pattern?

This comment has been minimized.

Copy link
@gafter

gafter Nov 20, 2015

Author Member

Perhaps.

This comment has been minimized.

Copy link
@alrz

alrz Nov 20, 2015

Member

Though, now that I thought about it, this can be useful like

FunctionWithALongName() case var result : <expression-using-result>
//instead of 
(var result = FunctionWithALongName() ; <expression-using-result>)

I don't know which one would you prefer.

This comment has been minimized.

Copy link
@gafter

gafter Nov 20, 2015

Author Member

However, I don't think this syntax works more generally. It will not diagnose the missing comma if there is only one missing comma in a switch-expression, for example.

This comment has been minimized.

Copy link
@alrz

alrz Nov 20, 2015

Member

Your example in #3718 (comment) could be easily written with case,

CostlyFunction(x) switch (case var tmp: new {
   A = tmp.Field1,
   B = tmp.Field2,
   C = tmp.Field3 == 5 ? tmp.Field1 : tmp.Field2
})
CostlyFunction(x) case var tmp: new {
   A = tmp.Field1,
   B = tmp.Field2,
   C = tmp.Field3 == 5 ? tmp.Field1 : tmp.Field2
}

So it has some use cases. That was what I wanted to know. Thanks.

This comment has been minimized.

Copy link
@gafter

gafter Nov 21, 2015

Author Member

If your idea of a "use case" is eliminating a couple of tokens, sure.

This comment has been minimized.

Copy link
@alrz

alrz Nov 21, 2015

Member

I was the one that asked why it is chosen to be pattern now I'm defending it 😄 anyway I think it's nice that variable declaration is closer to its usage.

This comment has been minimized.

Copy link
@gafter

gafter Nov 21, 2015

Author Member

@alrz, I guess I forgot the original question :/

;
```

Expand Down Expand Up @@ -492,9 +492,9 @@ Expr Simplify(Expr e)
var areas =
from primitive in primitives
let area = primitive switch (
case Line l: 0
case Rectangle r: r.Width * r.Height
case Circle c: Math.PI * c.Radius * c.Radius
case Line l: 0,
case Rectangle r: r.Width * r.Height,
case Circle c: Math.PI * c.Radius * c.Radius,
case *: throw new ApplicationException()
)
select new { Primitive = primitive, Area = area };
Expand Down

0 comments on commit 9ee1b98

Please sign in to comment.