Skip to content

Commit 277d208

Browse files
sjrdKordyjan
authored andcommitted
Spec: Syntax of control structures.
[Cherry-picked cbf9d42]
1 parent 8b3469a commit 277d208

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

docs/_spec/06-expressions.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ chapter: 6
1010
Expr ::= (Bindings | id | ‘_’) ‘=>’ Expr
1111
| Expr1
1212
Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr]
13+
| ‘if‘ Expr ‘then‘ Expr [[semi] ‘else‘ Expr]
1314
| ‘while’ ‘(’ Expr ‘)’ {nl} Expr
14-
| ‘try’ Expr [‘catch’ Expr] [‘finally’ Expr]
15-
| ‘for’ (‘(’ Enumerators ‘)’ | ‘{’ Enumerators ‘}’) {nl} [‘yield’] Expr
15+
| ‘while’ Expr ‘do’ Expr
16+
| ‘try’ Expr [Catches] [‘finally’ Expr]
17+
| ‘for’ (‘(’ Enumerators ‘)’ | ‘{’ Enumerators ‘}’) {nl} [‘do‘ | ‘yield’] Expr
18+
| ‘for’ Enumerators (‘do‘ | ‘yield’) Expr
1619
| ‘throw’ Expr
1720
| ‘return’ [Expr]
1821
| [SimpleExpr ‘.’] id ‘=’ Expr
@@ -46,6 +49,7 @@ ResultExpr ::= Expr1
4649
Ascription ::= ‘:’ InfixType
4750
| ‘:’ Annotation {Annotation}
4851
| ‘:’ ‘_’ ‘*’
52+
Catches ::= ‘catch‘ (Expr | ExprCaseClause)
4953
```
5054

5155
Expressions are composed of operators and operands.
@@ -681,6 +685,7 @@ def matmul(xss: Array[Array[Double]], yss: Array[Array[Double]]) = {
681685

682686
```ebnf
683687
Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr]
688+
| ‘if‘ Expr ‘then‘ Expr [[semi] ‘else‘ Expr]
684689
```
685690

686691
The _conditional expression_ `if (´e_1´) ´e_2´ else ´e_3´` chooses one of the values of ´e_2´ and ´e_3´, depending on the value of ´e_1´.
@@ -700,6 +705,7 @@ The conditional expression `if (´e_1´) ´e_2´` is evaluated as if it was `if
700705

701706
```ebnf
702707
Expr1 ::= ‘while’ ‘(’ Expr ‘)’ {nl} Expr
708+
| ‘while’ Expr ‘do’ Expr
703709
```
704710

705711
The _while loop expression_ `while (´e_1´) ´e_2´` is typed and evaluated as if it was an application of `whileLoop (´e_1´) (´e_2´)` where the hypothetical method `whileLoop` is defined as follows.
@@ -874,15 +880,19 @@ The type of a throw expression is `scala.Nothing`.
874880
## Try Expressions
875881

876882
```ebnf
877-
Expr1 ::= ‘try’ Expr [‘catch’ Expr] [‘finally’ Expr]
883+
Expr1 ::= ‘try’ Expr [Catches] [‘finally’ Expr]
884+
885+
Catches ::= ‘catch‘ (Expr | ExprCaseClause)
878886
```
879887

880-
A _try expression_ is of the form `try { ´b´ } catch ´h´` where the handler ´h´ is usually a [pattern matching anonymous function](08-pattern-matching.html#pattern-matching-anonymous-functions)
888+
A _try expression_ is of the form `try ´b´ catch ´h´` where the handler ´h´ is usually a [pattern matching anonymous function](08-pattern-matching.html#pattern-matching-anonymous-functions)
881889

882890
```scala
883891
{ case ´p_1´ => ´b_1´ ... case ´p_n´ => ´b_n´ }
884892
```
885893

894+
If the handler is a single `ExprCaseClause`, it is a shorthand for that `ExprCaseClause` wrapped in a pattern matching anonymous function.
895+
886896
This expression is evaluated by evaluating the block ´b´.
887897
If evaluation of ´b´ does not cause an exception to be thrown, the result of ´b´ is returned.
888898
Otherwise the handler ´h´ is applied to the thrown exception.
@@ -891,22 +901,22 @@ If the handler contains no case matching the thrown exception, the exception is
891901
More generally, if the handler is a `PartialFunction`, it is applied only if it is defined at the given exception.
892902

893903
Let ´\mathit{pt}´ be the expected type of the try expression.
894-
The block ´b´ is expected to conform to ´\mathit{pt}´.
904+
The expression ´b´ is expected to conform to ´\mathit{pt}´.
895905
The handler ´h´ is expected conform to type `scala.Function[scala.Throwable, ´\mathit{pt}\,´]`.
896906
The type of the try expression is the [least upper bound](03-types.html#least-upper-bounds-and-greatest-lower-bounds) of the type of ´b´ and the result type of ´h´.
897907

898-
A try expression `try { ´b´ } finally ´e´` evaluates the block ´b´.
908+
A try expression `try ´b´ finally ´e´` evaluates the expression ´b´.
899909
If evaluation of ´b´ does not cause an exception to be thrown, the expression ´e´ is evaluated.
900910
If an exception is thrown during evaluation of ´e´, the evaluation of the try expression is aborted with the thrown exception.
901911
If no exception is thrown during evaluation of ´e´, the result of ´b´ is returned as the result of the try expression.
902912

903913
If an exception is thrown during evaluation of ´b´, the finally block ´e´ is also evaluated.
904914
If another exception ´e´ is thrown during evaluation of ´e´, evaluation of the try expression is aborted with the thrown exception.
905915
If no exception is thrown during evaluation of ´e´, the original exception thrown in ´b´ is re-thrown once evaluation of ´e´ has completed.
906-
The block ´b´ is expected to conform to the expected type of the try expression.
916+
The expression ´b´ is expected to conform to the expected type of the try expression.
907917
The finally expression ´e´ is expected to conform to type `Unit`.
908918

909-
A try expression `try { ´b´ } catch ´e_1´ finally ´e_2´` is a shorthand for `try { try { ´b´ } catch ´e_1´ } finally ´e_2´`.
919+
A try expression `try ´b´ catch ´e_1´ finally ´e_2´` is a shorthand for `try { try ´b´ catch ´e_1´ } finally ´e_2´`.
910920

911921
## Anonymous Functions
912922

docs/_spec/08-pattern-matching.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ Therefore, the right hand side of the case clause, `y.n`, of type `Int`, is foun
489489
Expr ::= PostfixExpr ‘match’ ‘{’ CaseClauses ‘}’
490490
CaseClauses ::= CaseClause {CaseClause}
491491
CaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Block
492+
ExprCaseClause ::= ‘case’ Pattern [Guard] ‘=>’ Expr
492493
```
493494

494495
A _pattern matching expression_

0 commit comments

Comments
 (0)