|
|
|
|
|
by delhanty
3451 days ago
|
|
Thanks for this. I don't fully understand your code yet: Is it the case that the type-safety of your approach relies on the caller honouring the convention of accessing Result<T,E> via the try and try_chain macros? I really need to look in detail at how Rust enforces type-safety of Result<T,E>. One thing that I like about how algebraic data types work in functional languages (e.g. Either in Haskell) is that they eliminate entirely the need for null pointers completely and the possibility of derefencing them (Tony Hoare's billion dollar mistake) and I had assumed that Rust would give me the same. |
|
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.