logoalt Hacker News

tialaramextoday at 3:18 PM5 repliesview on HN

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.


Replies

merbtoday at 5:22 PM

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.

show 1 reply
CharlieDigitaltoday at 4:36 PM

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

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

layer8today at 6:41 PM

> 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>.

rafaelmntoday at 5:16 PM

> OneOrMoreOrNone

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

paulddrapertoday at 5:41 PM

I went to prove you wrong…

And you’re exactly right.

It’s not “one or more.”

It’s “one or not one.”

Need two or not two.

show 1 reply