scope(exit) with expected library

Paul Backus snarwin at gmail.com
Wed Aug 25 14:22:26 UTC 2021


On Wednesday, 25 August 2021 at 14:04:54 UTC, WebFreak001 wrote:
> Would it be possible to extend `scope(exit)` and 
> `scope(success)` to trigger properly for functions returning 
> `Expected!T` as defined in the 
> [expectations](https://code.dlang.org/packages/expectations) 
> and [expected](https://code.dlang.org/packages/expected) DUB 
> libraries?
>
> For example is it possible to make this work as expected:
> ```d
> Expected!int divide(int a, int b) nothrow
> {
>     scope (failure) writeln("division failed");
>     scope (success) writeln("division succeeded");
>
>     if (b == 0) return err!int("division by zero");
>     return ok(a / b);
> }
> ```

Probably the only principled way to make this work would be to 
define some kind of "concept"/structural interface that's 
recognized by the compiler to mean "this is an error-handling 
type", in the same way that the compiler recognizes 
`empty`/`front`/`popFront` to mean "this is an iterable type".

Even then, it would require some pretty invasive language changes 
(and some pretty gnarly code in the compiler), but it's at least 
*theoretically* possible.


More information about the Digitalmars-d-learn mailing list