← Back to context

Comment by tialaramex

7 hours ago

I don't love OneOrMore<T>

It's trying to generalize - we might have exactly one T, fine, or a collection of T, and that's more T... except no, the collection might be zero of them, not at least one and so our type is really "OneOrMoreOrNone" and wow, that's just maybe some T.

OneOrMore is more or less an example from the functional world. i.e.:

https://hackage.haskell.org/package/oneormore or scala: https://typelevel.org/cats/datatypes/nel.html

it's for type purists, because sometimes you want the first element of the list but if you do that you will get T? which is stupid if you know that the list always holds an element, because now you need to have an unnecessary assertion to "fix" the type.

  • The NonEmptyList in Cats is a product (struct/tuple) type, though; I assume the Haskell version is the same. The type shown in the blog post is a sum (union) type which can contain an empty enumerable, which contradicts the name OneOrMore. The use described for the type in the post (basically a convenience conversion funnel) is different and makes sense in its own right (though it feels like kind of a weak use case). I'm not sure what a good name would've been illustratively that would've been both accurate and not distracting, though.

    • Well you are right of course, I just wanted to explain what they wanted to show. Of course the type would be wrong if the second entry in itself is an empty list. I just wanted to explain the reasoning what they tried to accomplish

      They could’ve done the Either type which would’ve been more correct or maybe EitherT (if the latter is even possible)

      1 reply →

`OneOrMore<T>` was an example of using `union` types.

You are free to call it `public union Some<T>(T, IEnumerable<T>)`

> so our type is really "OneOrMoreOrNone"

If I understand correctly, it’s actually OneOrOneOrMoreOrNone. Because you have two different distinguishable representations of “one”.

The only reason to use this would be if you typically have exactly one, and you want to avoid the overhead of an enumeration in that typical case. In other words, AnyNumberButOftenJustOne<T>.

> OneOrMoreOrNone

So IEnumerable<T> ? What's up with wrapping everything into fancy types just to arrive at the exact same place.