Monads in C# (Part 2): Result
alexyorke.github.io
Monads in C# (Part 2): Result
1–10 of 75 posts
Re: Monads in C# (Part 2): Result
#2Re: Monads in C# (Part 2): Result
#3I've been playing around with some of the "standard/common monads in C# for a while now, in OSS ( https://github.com/pimbrouwers/Danom ) and at work. It's awesome. I can't imagine working without them anymore.
Re: Monads in C# (Part 2): Result
#4I've been playing around with some of the "standard/common monads in C# for a while now, in OSS ( https://github.com/pimbrouwers/Danom ) and at work. It's awesome. I can't imagine working without them anymore.
Thanks for sharing! Appreciate the OSS work. Is it ok if I reference that repo in my article?
Re: Monads in C# (Part 2): Result
#5 Result
looks extremely ugly and unergonomic, even just to type.I understand that this is complicated topic and there were a lot of strong opinions even inside of Google about it, but god, I miss absl::StatusOr and ASSIGN_OR_RETURN. Yes, it won’t work without preprocessor magic (and that’s why this article goes through heavy functional stuff, otherwise it just cannot work in language like C#), but it’s so easy and natural to use in base case, it feels like cheating.
Re: Monads in C# (Part 2): Result
#6Idk, to me that constant Result looks extremely ugly and unergonomic, even just to type. I understand that this is complicated topic and there were a lot of strong opinions even inside of Google about it, but god, I miss absl::StatusOr and ASSIGN_OR_RETURN. Yes, it won’t work without preprocessor magic (and that’s why this article goes through heavy functional stuff, otherwise it just cannot work in language like C#)…
The static method approach showcased in the article is really long-winded.
Re: Monads in C# (Part 2): Result
#7Idk, to me that constant Result looks extremely ugly and unergonomic, even just to type. I understand that this is complicated topic and there were a lot of strong opinions even inside of Google about it, but god, I miss absl::StatusOr and ASSIGN_OR_RETURN. Yes, it won’t work without preprocessor magic (and that’s why this article goes through heavy functional stuff, otherwise it just cannot work in language like C#)…
I think in C# the way to solve this is to have two separate types, `Ok ` and `Err `, and provide implicit conversions for both to `Result `. The static method approach showcased in the article is really long-winded.
With implicit type parameters this boils down to Ok(4) or BadRequest()
Re: Monads in C# (Part 2): Result
#8[^1]: https://odr.chalmers.se/items/91bf8c4b-93dd-43ca-8ac2-8b0d2c...
[^2]: https://github.com/master-of-monads/monads-cs/blob/89netram/...
Re: Monads in C# (Part 2): Result
#9I've been playing around with some of the "standard/common monads in C# for a while now, in OSS ( https://github.com/pimbrouwers/Danom ) and at work. It's awesome. I can't imagine working without them anymore.
Yours looks a lot more idiomatic to C# (hence acceptable for a mixed code base) but the above linked more "systematic". Not that I have used any or have any competence.
Re: Monads in C# (Part 2): Result
#10 try
{
id = int.Parse(inputId);
}
catch (Exception ex) when (ex is FormatException or OverflowException)
{
throw new InvalidOperationException("DeactivateUser failed at: parse id", ex);
}
Where all you're doing when you catch an exception is throwing it in a more generic way. You could just let the FormatException or OverflowException bubble up, so the parent can handle those differently if needed. If you want to hide that implementation detail, then you should still consider throwing more specific types of exception for different errors. Just throwing InvalidOperationException for everything feels lazy.You've gone out your way to destroy the information being passed up when using exceptions, to demonstrate the value in having error types.
It would be far more conventional to also provide a `TryDeactivateUser` function that cannot throw an exception. The article does note this, but hand-waves it away.
I'm not against Result types, but I don't find this article entirely convincing.