(Note that the typedefs aren't needed, but I prefer to be explicit there.)
The try macro is just trying to emulate try! and it is not really needed.
We may access the result by hand and the correct type will be used, but it is way uglier (see: https://github.com/fungos/sllab/blob/master/src/viewer.cpp#L...) due the access on the struct members .e (error) or .v (value). And if I try to assign .v to another type, the compiler will give a compile time error.
And probably would be better to use std::variant there as it is more like a tagged union, I just haven't tried to improve on it anymore.
Also, note that my unwrap doesn't panic, I should put a test/assert there...
Anyway, the poc is there and it should possible to have something almost as clean as Rust with some more work.
You can see somewhere there I do this:
I could also have done: In this example there are two instantiations of the type Result<T, E>: (Note that the typedefs aren't needed, but I prefer to be explicit there.)The try macro is just trying to emulate try! and it is not really needed. We may access the result by hand and the correct type will be used, but it is way uglier (see: https://github.com/fungos/sllab/blob/master/src/viewer.cpp#L...) due the access on the struct members .e (error) or .v (value). And if I try to assign .v to another type, the compiler will give a compile time error.
And probably would be better to use std::variant there as it is more like a tagged union, I just haven't tried to improve on it anymore.
Also, note that my unwrap doesn't panic, I should put a test/assert there...
Anyway, the poc is there and it should possible to have something almost as clean as Rust with some more work.