true-myth / true-myth-csharp

A library for safer optional programming in C♯.
https://true-myth.github.io/true-myth-csharp
MIT License
4 stars 1 forks source link

Convert Maybe and Result into value types #20

Closed aggieben closed 5 years ago

aggieben commented 5 years ago

Currently Maybe and Result are implemented as reference types - that is, regular classes. However, I'd like to see these be value types; they would be better off as value types (struct in C♯) because it would be a compiler-enforced guarantee that they would never be null. Secondarily it's plausible that there could be a small performance win by doing that, but that's not the motivation here.

aggieben commented 5 years ago

After thinking about this a bit more, I think this would actually be a net loss in terms of tradeoffs. A happy coincidence is that an uninitialized Maybe<T> is the same as Maybe<T>.Nothing. The problem is that there's no way to prevent a user from getting an uninitialized Result<T,E>, which is incoherent. I'd rather it be possible to have a typed Maybe<T> or Result<T,E> variable be set to null than to get an initialized, nonsensical Result<T,E>. Furthermore, non-nullable reference types are coming in C# 8.0, which will allow us to keep the current behavior and prevent null references, which is the best of both. I'm content to just wait.