Created
June 12, 2015 09:38
-
-
Save kmorcinek/2d472e6be2502d82f4df to your computer and use it in GitHub Desktop.
Either<TSuccess, TFailure>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Either<TSuccess, TFailure> | |
{ | |
private readonly TSuccess _success; | |
private readonly TFailure _failure; | |
private readonly bool _isSuccessful; | |
public bool Succeeded | |
{ | |
get | |
{ | |
return this._isSuccessful; | |
} | |
} | |
public TSuccess SuccessValue | |
{ | |
get | |
{ | |
if (!this._isSuccessful) | |
throw new InvalidOperationException("Cannot get a successful result when in a failure state."); | |
return this._success; | |
} | |
} | |
public TFailure FailureValue | |
{ | |
get | |
{ | |
if (this._isSuccessful) | |
throw new InvalidOperationException("Cannot get a failure result when in a success state."); | |
return this._failure; | |
} | |
} | |
public Either(TSuccess success) | |
{ | |
this._success = success; | |
this._isSuccessful = true; | |
this._failure = default(TFailure); | |
} | |
public Either(TFailure failure) | |
{ | |
this._success = default(TSuccess); | |
this._isSuccessful = false; | |
this._failure = failure; | |
} | |
public static implicit operator Either<TSuccess, TFailure>(TSuccess value) | |
{ | |
return new Either<TSuccess, TFailure>(value); | |
} | |
public static implicit operator Either<TSuccess, TFailure>(TFailure value) | |
{ | |
return new Either<TSuccess, TFailure>(value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment