Conversation
Previously, parsing `try` was special-cased for expressions
beginning with paren or brace. Perhaps that was to avoid odd
sights such as `try (1,2,3)` or `try { case _ => }`, but it
also broke `try { 1 } + 1 finally ()`.
This commit takes an arbitrary expression to `try`.
It also takes an arbitrary expression for `catch`. But for
improved type safety, the expression is required to conform
to `PartialFunction[Throwable, ?]`. The previous transform
was named-based. In addition, this commit invokes `applyOrElse`.
Previously:
```
scala> try 42 catch new {
| def isDefinedAt(x: Any) = false
| def apply(x: Any) = ()
| }
warning: there was one feature warning; re-run with -feature for details
res0: AnyVal = 42
scala> try 42 catch 22
<console>:8: error: value isDefinedAt is not a member of Int
try 42 catch 22
^
```
Now:
```
scala> try 42 catch new {
| def isDefinedAt(x: Any) = false
| def apply(x: Any) = ()
| }
<console>:8: error: type mismatch;
found : AnyRef{}
required: PartialFunction[Throwable,?]
try 42 catch new {
^
scala> try 42 catch 22
<console>:8: error: type mismatch;
found : Int(22)
required: PartialFunction[Throwable,?]
try 42 catch 22
^
```
The syntax allows arbitrary expressions for both try and catch. It is already specified that the expected type of a handler is a PartialFunction.
|
This patch introduces one or two hygiene problems: |
|
In the process of contrasting this translation with the status quo, I've uncovered another bug. I've assigned this to you as it will be fixed if we pursue the proposed approach in this PR (but I would ask you to add the regression test.) |
|
I would suggest that the inference-assisting version of |
There was a problem hiding this comment.
nme.applyOrElse. Generally it is better to reuse existing names to avoid repetitive name table lookups.
|
Thanks for taking a look. I guess there's a real incentive for slender trees going forward. At least the "inline" was rooted. I'll refresh my memory and make another pass. That |
|
Another motivation for avoiding introducing functions and local methods in translations is that these can sometimes be outlawed or trigger |
Previously, parsing
trywas special-cased for expressionsbeginning with paren or brace. Perhaps that was to avoid odd
sights such as
try (1,2,3)ortry { case _ => }, but italso broke
try { 1 } + 1 finally ().This commit takes an arbitrary expression to
try.It also takes an arbitrary expression for
catch. But forimproved type safety, the expression is required to conform
to
PartialFunction[Throwable, ?]. The previous transformwas named-based. In addition, this commit invokes
applyOrElse.Previously:
Now: