-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathEither.cs
More file actions
178 lines (155 loc) · 3.4 KB
/
Copy pathEither.cs
File metadata and controls
178 lines (155 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Date: 14.7.2015, Time: 1:20 */
using System;
namespace IllidanS4.SharpUtils.ObjectModel
{
/// <summary>
/// Contains a value of type TFirst or a value of type TSecond.
/// Conforms to ECMA TR/89.
/// </summary>
public struct Either<TFirst, TSecond> : IEquatable<Either<TFirst, TSecond>>, IEquatable<TFirst>//, IEquatable<TSecond>
{
readonly bool isSecond;
readonly TFirst first;
readonly TSecond second;
public TFirst First{
get{
if(!isSecond) return first;
throw new InvalidOperationException("The object doesn't contain this value.");
}
}
public TSecond Second{
get{
if(isSecond) return second;
throw new InvalidOperationException("The object doesn't contain this value.");
}
}
public object Value{
get{
if(isSecond) return second;
else return first;
}
}
public bool IsFirst{
get{
return !isSecond;
}
}
public bool IsSecond{
get{
return isSecond;
}
}
public Either(TFirst first) : this()
{
this.first = first;
isSecond = false;
}
public Either(TSecond second) : this()
{
this.second = second;
isSecond = true;
}
public override bool Equals(object other)
{
if(other == null)
{
return false;
}else if(other is Either<TFirst, TSecond>)
{
return Equals((Either<TFirst, TSecond>)other);
}else{
return false;
}
}
public bool Equals(Either<TFirst, TSecond> other)
{
if(!isSecond && !other.isSecond)
{
var eq = first as IEquatable<TFirst>;
if(eq != null)
{
return eq.Equals(other.first);
}
return first.Equals(other.first);
}else if(isSecond && other.isSecond)
{
var eq = second as IEquatable<TFirst>;
if(eq != null)
{
return eq.Equals(other.first);
}
return second.Equals(other.second);
}else{
return false;
}
}
public bool Equals(TFirst other)
{
return EqualsElement(other);
}
public bool Equals(TSecond other)
{
return EqualsElement(other);
}
private bool EqualsElement<TArg>(TArg other)
{
var val = Value;
var eq = val as IEquatable<TArg>;
if(eq != null)
{
return eq.Equals(other);
}
return val.Equals(other);
}
public override int GetHashCode()
{
if(isSecond)
{
return second.GetHashCode();
}else{
return first.GetHashCode();
}
}
public override string ToString()
{
if(isSecond)
{
return second.ToString();
}else{
return first.ToString();
}
}
public static bool operator ==(Either<TFirst, TSecond> left, Either<TFirst, TSecond> right)
{
return left.Equals(right);
}
public static bool operator !=(Either<TFirst, TSecond> left, Either<TFirst, TSecond> right)
{
return !left.Equals(right);
}
public static Either<TFirst, TSecond> MakeFirst(TFirst first)
{
return new Either<TFirst, TSecond>(first);
}
public static Either<TFirst, TSecond> MakeSecond(TSecond second)
{
return new Either<TFirst, TSecond>(second);
}
public static explicit operator TFirst(Either<TFirst, TSecond> value)
{
return value.First;
}
public static explicit operator TSecond(Either<TFirst, TSecond> value)
{
return value.Second;
}
public static implicit operator Either<TFirst, TSecond>(TFirst first)
{
return MakeFirst(first);
}
public static implicit operator Either<TFirst, TSecond>(TSecond second)
{
return MakeSecond(second);
}
}
}