Try blocks are trying
FeepingCreature
feepingcreature at gmail.com
Mon Oct 11 07:48:15 UTC 2021
On Sunday, 10 October 2021 at 16:12:31 UTC, Carl Sturtivant wrote:
> Why is it that a try block is defined to establish a scope?
>
> ```
> try {
> auto x = frgl();
> }
> // ...
> // x undefined here
>
> ```
> What is the benefit of this? If try (like static if/foreach)
> did NOT establish a scope, then this annoyance would go away.
> And if a scope was desired for some reason, then an extra pair
> of braces could be written within.
>
> Right now rewriting the above requires getting the return type
> of frgl and declaring x to be of that type before the try block.
>
> Design error?
I do not deny that my heart has greatly desired this.
Proposal:
```
export try {
auto x = frgl();
}
catch (Exception) {
return false;
}
```
shall be equivalent to
```
typeof(frgl()) x = void;
try {
frgl().moveEmplace(x);
}
catch (Exception) {
__EXCEPTION_BODY__
assert(false, "`export` specified: all catch blocks must exit
scope");
}
POST
```
Here, `auto frgl` dominates `POST` and is thus correct.
Scope guards directly in `try{}` should be lifted into the
surrounding context, but this is confusing, so they are an error.
More information about the Digitalmars-d
mailing list