14 public static class Maybe {
39 public static void MatchAll<A, B, C, D>(
Maybe<A> maybeA,
Maybe<B> maybeB,
Maybe<C> maybeC,
Maybe<D> maybeD, Action<A, B, C, D> action) {
59 public struct Maybe<T> : IEquatable<Maybe<T>>, IComparable, IComparable<Maybe<T>> {
84 private readonly T _t;
92 if (Type<T>.isValueType) {
105 if (!Type<T>.isValueType && t ==
null) {
106 throw new ArgumentNullException(
"Cannot use Some with a null argument.");
124 public void Match(Action<T> ifValue) {
134 public void Match(Action<T> ifValue, Action ifNot) {
136 if (ifValue !=
null) ifValue(_t);
146 public K
Match<K>(Func<T, K> ifValue, Func<K> ifNot) {
148 if (ifValue !=
null) {
166 return customDefault;
181 return maybeCustomDefault;
187 return Values.Single(_t);
189 return Values.Empty<T>();
194 return hasValue ? _t.GetHashCode() : 0;
197 public override bool Equals(
object obj) {
209 return _t.Equals(other._t);
217 throw new ArgumentException();
227 IComparable<T> ct = _t as IComparable<T>;
229 return ct.CompareTo(other._t);
231 IComparable c = _t as IComparable;
233 return c.CompareTo(other._t);
244 return maybe0.Equals(maybe1);
248 return !maybe0.Equals(maybe1);
252 return maybe0.CompareTo(maybe1) > 0;
256 return maybe0.CompareTo(maybe1) >= 0;
260 return maybe0.CompareTo(maybe1) < 0;
264 return maybe0.CompareTo(maybe1) <= 0;
A struct that represents a value that could or could not exist. Unlike the built-int nullable types,...
bool Equals(Maybe< T > other)
static void MatchAll< A, B, C >(Maybe< A > maybeA, Maybe< B > maybeB, Maybe< C > maybeC, Action< A, B, C > action)
T ValueOr(T customDefault)
If this Maybe has a value, returns the value, otherwise returns the argument custom default value.
static readonly NoneType None
Maybe< T > ValueOr(Maybe< T > maybeCustomDefault)
Returns this Maybe if it has a value, otherwise returns the argument Maybe value. Useful for overlayi...
static void MatchAll< A, B, C, D >(Maybe< A > maybeA, Maybe< B > maybeB, Maybe< C > maybeC, Maybe< D > maybeD, Action< A, B, C, D > action)
void Match(Action< T > ifValue, Action ifNot)
If this Maybe has a value, the first delegate is called with that value, else the second delegate is ...
static void MatchAll< A, B >(Maybe< A > maybeA, Maybe< B > maybeB, Action< A, B > action)
void Match(Action< T > ifValue)
If this Maybe has a value, the delegate is called with that value.
readonly bool hasValue
Returns whether or not this Maybe contains a value.
static Maybe< T > Some(T t)
Constructs a Maybe given a specific value. This value needs to always be non-null if the type is a re...
static Maybe< T > Some< T >(T value)
override int GetHashCode()
static bool operator<=(Maybe< T > maybe0, Maybe< T > maybe1)
T valueOrDefault
Gets the value, or the type's default if it doesn't exist.
static bool operator>=(Maybe< T > maybe0, Maybe< T > maybe1)
Maybe(T t)
Constructs a Maybe given a value. If the value is not null, this maybe will have a value....
int CompareTo(object obj)
static bool operator==(Maybe< T > maybe0, Maybe< T > maybe1)
static bool operator<(Maybe< T > maybe0, Maybe< T > maybe1)
int CompareTo(Maybe< T > other)
override bool Equals(object obj)
static bool operator!=(Maybe< T > maybe0, Maybe< T > maybe1)
static bool operator>(Maybe< T > maybe0, Maybe< T > maybe1)
bool TryGetValue(out T t)
If this Maybe has a value, the out argument is filled with that value and this method returns true,...
K Match< K >(Func< T, K > ifValue, Func< K > ifNot)
If this Maybe has a value, the first delegate is called with that value, else the second delegate is ...
A Query object is a type of immutable ordered collection of elements that can be used to perform usef...