Discussion Thread: DIP 1029--Add throw as Function Attribute--Final Review

Paul Backus snarwin at gmail.com
Sat Apr 18 14:02:46 UTC 2020


On Saturday, 18 April 2020 at 12:03:46 UTC, Jacob Carlborg wrote:
> Result!(void, CopyError) copy(string src, string dest)
> {
>     return Result!(void, CopyError)(CopyError.permissionDenied);
> }
>
> void main()
> {
>     auto result = copy("foo", "bar");
>
>     if (!result.isValue)
>         goto L1;
>     L1:
>         writeln(result.error);
> }

The big downside of this compared to exceptions is that you can 
forget to check the return value of `copy`, whereas exceptions 
cannot be ignored.

In Rust and C++, they have `#[must_use]` and `[[nodiscard]]` 
respectively to force the programmer to check the return value. 
Without something equivalent in D, using algebraic data types for 
error handling is not really a viable approach.


More information about the Digitalmars-d mailing list