Worst ideas/features in programming languages?

bauss jj_1337 at live.dk
Tue Nov 9 10:58:34 UTC 2021


On Monday, 8 November 2021 at 14:23:15 UTC, Ola Fosheim Grøstad 
wrote:
> On Monday, 8 November 2021 at 14:08:32 UTC, Atila Neves wrote:
>> I thought Rust error handling wasn't easy by default until 
>> they added the ? operator, at which point it became like 
>> exceptions but better.
>
> Looks like syntactical sugar to me, but I am no Rust expert. It 
> means you now loose context, and how do you log?
>

It propagates the error to the caller, so you don't lose context, 
as the error still needs to be handled somewhere, you just don't 
explicitly have to return the error from your function etc. If 
you need to log the error at the specific location etc. then you 
just don't use the ? operator.

If the same mechanic existed in D then it would be something like:

```d
Result!(int,Error) bar();

Result!(int,Error) foo(int x) {
     auto i = bar()?;

     return result(i * x);
}

void main() {
     auto r = foo(10);

     if (r.error) {
         // Error ...
     } else {
         writeln(r.value);
     }
}
```

Which would translate to:

```d
Result!(int,Error) bar();

Result!(int,Error) foo(int x) {
     auto r = bar();

     if (r.error) return r;

     auto i = r.value;

     return result(i * x);
}

void main() {
     auto r = foo(10);

     if (r.error) {
         // Error ...
     } else {
         writeln(r.value);
     }
}
```

Of course the if statements would be using pattern matching for 
Ok and Error, which it does in Rust. But felt lazy.


More information about the Digitalmars-d mailing list