4 ms·
I might be missing something. Wouldn't the C# solution to keep the wrap messages be to add a MapError method? Something like: public ErrorChecked<T, E> Ma
by taberiand 4y ago
I might be missing something. Wouldn't the C# solution to keep the wrap messages be to add a MapError method? Something like:
public ErrorChecked<T, E> MapError(Func<E, E> op) =>
_value is not null
? Value(_value)
: Error(op(_error!));
Used like
var result =
from thing1 in DoFirstThing().MapError(ex => new Exception("Failed doing first thing", ex))
from thing2 in DoSecondThing(thing1).MapError(ex => new Exception("Failed doing second thing", ex))
select thing2;
Which, maybe it's just me, but I think that looks cleaner than the Go code.
- lalaithion 4y agoYes, that is the solution; I'm complaining that it wasn't even mentioned as a possibility in the blog post.
- taberiand 4y agoOh right, fair enough. I usually end up doing the Go style in C# anyway; the from ... syntax usually leaves other devs unconvinced and suspicious.
- lalaithion 4y agoHaskell could have (I don't know a library that implements this, but it's ~trivial) do x <- open filename `errorContext` ("failed to open " <> filename) y <- readBytes x `errorContext` "failed to read bytes from file" Rust has let x = open(filename).with_context(|| format!("failed to open {}", filename)?; let y = read_bytes(x).with_context(|| format!("failed to read bytes from file")?; or if we ever get https://github.com/joshtriplett/rfcs/blob/simple-postfix-macros/text/2442-simple-postfix-macros.md https://github.com/joshtriplett/rfcs/blob/simple-postfix-mac..., let x = open(filename).with_context!("failed to open {}", filename)?; let y = read_bytes(x).with_context!("failed to read bytes from file")?;