Tanoda
Either.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10
11namespace Leap.Unity {
12
18 public struct Either<A, B> : IEquatable<Either<A, B>>, IComparable, IComparable<Either<A, B>> {
19
23 public readonly bool isA;
24
28 public bool isB {
29 get {
30 return !isA;
31 }
32 }
33
34 private readonly A _a;
35 private readonly B _b;
36
41 public Maybe<A> a {
42 get {
43 if (isA) {
44 return Maybe<A>.Some(_a);
45 } else {
46 return Maybe<A>.None;
47 }
48 }
49 }
50
55 public Maybe<B> b {
56 get {
57 if (isA) {
58 return Maybe<B>.None;
59 } else {
60 return Maybe<B>.Some(_b);
61 }
62 }
63 }
64
68 public Either(A a) {
69 if (a == null) {
70 throw new ArgumentNullException("Cannot initialize an Either with a null value.");
71 }
72
73 isA = true;
74 _a = a;
75 _b = default(B);
76 }
77
81 public Either(B b) {
82 if (b == null) {
83 throw new ArgumentNullException("Cannot initialize an Either with a null value.");
84 }
85
86 isA = false;
87 _b = b;
88 _a = default(A);
89 }
90
95 public void Match(Action<A> ifA, Action<B> ifB) {
96 if (isA) {
97 if (ifA != null) ifA(_a);
98 } else {
99 if (ifB != null) ifB(_b);
100 }
101 }
102
107 public bool TryGetA(out A a) {
108 a = _a;
109 return isA;
110 }
111
116 public bool TryGetB(out B b) {
117 b = _b;
118 return !isA;
119 }
120
121 public override int GetHashCode() {
122 if (isA) {
123 return _a.GetHashCode();
124 } else {
125 return _b.GetHashCode();
126 }
127 }
128
129 public override bool Equals(object obj) {
130 if (obj is Either<A, B>) {
131 return Equals((Either<A, B>)obj);
132 } else {
133 return false;
134 }
135 }
136
137 public bool Equals(Either<A, B> other) {
138 if (isA != other.isA) {
139 return false;
140 } else if (isA) {
141 return _a.Equals(other._a);
142 } else {
143 return _b.Equals(other._b);
144 }
145 }
146
147 public int CompareTo(object obj) {
148 if (!(obj is Either<A, B>)) {
149 throw new ArgumentException();
150 } else {
151 return CompareTo((Either<A, B>)obj);
152 }
153 }
154
155 public int CompareTo(Either<A, B> other) {
156 if (isA != other.isA) {
157 return isA ? -1 : 1;
158 } else if (isA) {
159 IComparable<A> ca = _a as IComparable<A>;
160 if (ca != null) {
161 return ca.CompareTo(other._a);
162 } else {
163 IComparable c = _a as IComparable;
164 if (c != null) {
165 return c.CompareTo(other._b);
166 } else {
167 return 0;
168 }
169 }
170 } else {
171 IComparable<B> cb = _b as IComparable<B>;
172 if (cb != null) {
173 return cb.CompareTo(other._b);
174 } else {
175 IComparable c = _b as IComparable;
176 if (c != null) {
177 return c.CompareTo(other._b);
178 } else {
179 return 0;
180 }
181 }
182 }
183 }
184
185 public static bool operator ==(Either<A, B> either0, Either<A, B> either1) {
186 return either0.Equals(either1);
187 }
188
189 public static bool operator !=(Either<A, B> either0, Either<A, B> either1) {
190 return !either0.Equals(either1);
191 }
192
193 public static bool operator >(Either<A, B> either0, Either<A, B> either1) {
194 return either0.CompareTo(either1) > 0;
195 }
196
197 public static bool operator >=(Either<A, B> either0, Either<A, B> either1) {
198 return either0.CompareTo(either1) >= 0;
199 }
200
201 public static bool operator <(Either<A, B> either0, Either<A, B> either1) {
202 return either0.CompareTo(either1) < 0;
203 }
204
205 public static bool operator <=(Either<A, B> either0, Either<A, B> either1) {
206 return either0.CompareTo(either1) <= 0;
207 }
208
209 public static implicit operator Either<A, B>(A a) {
210 return new Either<A, B>(a);
211 }
212
213 public static implicit operator Either<A, B>(B b) {
214 return new Either<A, B>(b);
215 }
216 }
217}
A struct that represents a value that could or could not exist. Unlike the built-int nullable types,...
Definition: Maybe.cs:59
static readonly NoneType None
Definition: Maybe.cs:15
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...
Definition: Maybe.cs:104
A data structure that represents either a value of type A or a value of type B. The value can never b...
Definition: Either.cs:18
override int GetHashCode()
Definition: Either.cs:121
int CompareTo(object obj)
Definition: Either.cs:147
static bool operator!=(Either< A, B > either0, Either< A, B > either1)
Definition: Either.cs:189
bool TryGetB(out B b)
If this either contains the value of B, the out argument is filled with that value and this method re...
Definition: Either.cs:116
readonly bool isA
Returns whether or not this Either contains the first value.
Definition: Either.cs:23
Either(B b)
Constructs an Either with a value of B.
Definition: Either.cs:81
void Match(Action< A > ifA, Action< B > ifB)
Calls the first delegate with the value of A if it is present, else calls the second delegate with th...
Definition: Either.cs:95
bool isB
Returns whether or not this Either contains the second value.
Definition: Either.cs:28
bool TryGetA(out A a)
If this either contains the value of A, the out argument is filled with that value and this method re...
Definition: Either.cs:107
static bool operator>=(Either< A, B > either0, Either< A, B > either1)
Definition: Either.cs:197
static bool operator>(Either< A, B > either0, Either< A, B > either1)
Definition: Either.cs:193
static bool operator<=(Either< A, B > either0, Either< A, B > either1)
Definition: Either.cs:205
int CompareTo(Either< A, B > other)
Definition: Either.cs:155
Maybe< B > b
Returns a Maybe that contains the value of B if it exists, or no value if it doesn't.
Definition: Either.cs:55
override bool Equals(object obj)
Definition: Either.cs:129
static bool operator<(Either< A, B > either0, Either< A, B > either1)
Definition: Either.cs:201
static bool operator==(Either< A, B > either0, Either< A, B > either1)
Definition: Either.cs:185
Maybe< A > a
Returns a Maybe that contains the value of A if it exists, or no value if it doesn't.
Definition: Either.cs:41
bool Equals(Either< A, B > other)
Definition: Either.cs:137
Either(A a)
Constructs an Either with a value of A.
Definition: Either.cs:68