[OT] - C++ exceptions are becoming more and more problematic
bauss
jj_1337 at live.dk
Fri Feb 25 10:04:00 UTC 2022
On Friday, 25 February 2022 at 00:30:49 UTC, H. S. Teoh wrote:
>
> Go, Zig, Rust, and Odin all require explicit checking for error
> returns. This makes them suffer from exactly the same problem
> as my C example
>
This is not entirely true, at least for Rust.
Ex.
```
fn foo(i: i32) -> Result<i32, Error> {
if i % 2 == 0 {
Ok(i)
} else {
Err(/* ... */)
}
}
fn bar(i: i32) -> Result<i32, Error> {
let i = match foo(i) {
Ok(i) => i,
Err(e) => return Err(e),
};
// do something with i
}
```
The function bar can be reduced to:
```
fn bar(i: i32) -> Result<i32, Error> {
let i = foo(i)?; // The magic happens here
// do something with i
}
```
More information about the Digitalmars-d
mailing list