DavidArno / SuccincT

Discriminated unions, pattern matching and partial applications for C#
MIT License
267 stars 15 forks source link

Add deconstruct to Option/Maybe to support eg (hasValue, value) = collection.TryFirst(...) #37

Closed DavidArno closed 6 years ago

DavidArno commented 7 years ago

Based on a suggestion from @pkanavos:

  1. Add a deconstruct to Option<T> (and Maybe<T> if it doesn't just work through implicit casting):
    public void Deconstruct<T>(out bool hasValue, out T value) => ...
  2. Look at possibly adding similar deconstructs to Success<T>, Either<T1, T2> and union types.
megafinz commented 7 years ago

What will value contain if hasValue is false?

DavidArno commented 7 years ago

default(T) would seem the obvious choice. I'd prefer the idea that its undefined, but out parameters have to be set, always, so that isn't an option.

If you think this inherently a bad idea though, I'm open to being convinced to drop it.

megafinz commented 7 years ago

I don't think that's a bad idea, but I feel like it should be consistent with how Option<T>.Value behaves. Right now it's determined to throw an exception if HasValue == false, so maybe Option<T>.Value should return default(T) as well in this case.

DavidArno commented 7 years ago

If I changed Option<T>.Value to return default<T> when there's no value, then that would be a breaking change. So the choices are:

  1. Wait until v4 and introduce that breaking change (in reality there need not be a wait, v4 could be the next release, though this would mean creating a major version release though for a minor feature).
  2. Add a Option<T>.ValueOrDefault property that behaves this way and leave Option<T>.Value, as is.
megafinz commented 7 years ago

Option<T>.ValueOrDefault seems legit.

DavidArno commented 6 years ago

Added to v3.1