Struct Erratic<T>
- Namespace
- LfrlAnvil.Functional
- Assembly
- LfrlAnvil.Functional.dll
Represents a generic result of an action that may throw an error.
public readonly struct Erratic<T> : IErratic, IEquatable<Erratic<T>>, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable
Type Parameters
T
Value type.
- Implements
-
IEquatable<Erratic<T>>IEnumerable<T>
- Inherited Members
- Extension Methods
Fields
Empty
Represents an empty erratic, without an error.
public static readonly Erratic<T> Empty
Field Value
- Erratic<T>
Properties
Count
Gets the number of elements in the collection.
public int Count { get; }
Property Value
- int
The number of elements in the collection.
HasError
Specifies whether or not this instance contains an error.
public bool HasError { get; }
Property Value
IsOk
Specifies whether or not this instance contains a value.
public bool IsOk { get; }
Property Value
Methods
Bind<T2>(Func<T, Erratic<T2>>)
Returns the result of the provided ok
delegate invocation when IsOk is equal to true,
otherwise returns this instance.
[Pure]
public Erratic<T2> Bind<T2>(Func<T, Erratic<T2>> ok)
Parameters
Returns
- Erratic<T2>
New Erratic<T> instance.
Type Parameters
T2
Result type.
Bind<T2>(Func<T, Erratic<T2>>, Func<Exception, Erratic<T2>>)
Returns the result of the provided ok
delegate invocation when IsOk is equal to true,
otherwise returns the result of the provided error
delegate invocation.
[Pure]
public Erratic<T2> Bind<T2>(Func<T, Erratic<T2>> ok, Func<Exception, Erratic<T2>> error)
Parameters
ok
Func<T, Erratic<T2>>Delegate to invoke when IsOk is equal to true.
error
Func<Exception, Erratic<T2>>Delegate to invoke when IsOk is equal to false.
Returns
- Erratic<T2>
New Erratic<T> instance.
Type Parameters
T2
Result type.
Equals(Erratic<T>)
Indicates whether the current object is equal to another object of the same type.
[Pure]
public bool Equals(Erratic<T> other)
Parameters
other
Erratic<T>An object to compare with this object.
Returns
Equals(object?)
Indicates whether this instance and a specified object are equal.
[Pure]
public override bool Equals(object? obj)
Parameters
obj
objectThe object to compare with the current instance.
Returns
- bool
true if
obj
and this instance are the same type and represent the same value; otherwise, false.
GetError()
Gets the underlying error.
[Pure]
public Exception GetError()
Returns
- Exception
Underlying error.
Exceptions
- ValueAccessException
When underlying error does not exist.
GetErrorOrDefault()
Gets the underlying error or null when it does not exist.
[Pure]
public Exception? GetErrorOrDefault()
Returns
- Exception
Underlying error or null when it does not exist.
GetHashCode()
Returns the hash code for this instance.
[Pure]
public override int GetHashCode()
Returns
- int
A 32-bit signed integer that is the hash code for this instance.
GetValue()
Gets the underlying value.
[Pure]
public T GetValue()
Returns
- T
Underlying value.
Exceptions
- ValueAccessException
When underlying value does not exist.
GetValueOrDefault()
Gets the underlying value or a default value when it does not exist.
[Pure]
public T? GetValueOrDefault()
Returns
- T
Underlying value or a default value when it does not exist.
GetValueOrDefault(T)
Gets the underlying value or a default value when it does not exist.
[Pure]
public T GetValueOrDefault(T defaultValue)
Parameters
defaultValue
TDefault value to return in case the underlying value does not exist.
Returns
- T
Underlying value or a default value when it does not exist.
IfError(Action<Exception>)
Invokes the provided error
delegate when HasError is equal to true,
otherwise does nothing.
public Nil IfError(Action<Exception> error)
Parameters
Returns
IfErrorOrDefault<T2>(Func<Exception, T2>)
Returns the result of the provided error
delegate invocation when HasError is equal to true,
otherwise returns a default value.
[Pure]
public T2? IfErrorOrDefault<T2>(Func<Exception, T2> error)
Parameters
Returns
- T2
Delegate invocation result or a default value.
Type Parameters
T2
Result type.
IfErrorOrDefault<T2>(Func<Exception, T2>, T2)
Returns the result of the provided error
delegate invocation when HasError is equal to true,
otherwise returns a default value.
[Pure]
public T2 IfErrorOrDefault<T2>(Func<Exception, T2> error, T2 defaultValue)
Parameters
error
Func<Exception, T2>Delegate to invoke when HasError is equal to true.
defaultValue
T2Value to return HasError is equal to false.
Returns
- T2
Delegate invocation result or a default value.
Type Parameters
T2
Result type.
IfError<T2>(Func<Exception, T2?>)
Returns the result of the provided error
delegate invocation when HasError
is equal to true, otherwise returns None.
[Pure]
public Maybe<T2> IfError<T2>(Func<Exception, T2?> error) where T2 : notnull
Parameters
Returns
Type Parameters
T2
Result type.
IfOk(Action<T>)
Invokes the provided ok
delegate when IsOk is equal to true, otherwise does nothing.
public Nil IfOk(Action<T> ok)
Parameters
Returns
IfOkOrDefault<T2>(Func<T, T2>)
Returns the result of the provided ok
delegate invocation when IsOk is equal to true,
otherwise returns a default value.
[Pure]
public T2? IfOkOrDefault<T2>(Func<T, T2> ok)
Parameters
Returns
- T2
Delegate invocation result or a default value.
Type Parameters
T2
Result type.
IfOkOrDefault<T2>(Func<T, T2>, T2)
Returns the result of the provided ok
delegate invocation when IsOk is equal to true,
otherwise returns a default value.
[Pure]
public T2 IfOkOrDefault<T2>(Func<T, T2> ok, T2 defaultValue)
Parameters
ok
Func<T, T2>Delegate to invoke when IsOk is equal to true.
defaultValue
T2Value to return IsOk is equal to false.
Returns
- T2
Delegate invocation result or a default value.
Type Parameters
T2
Result type.
IfOk<T2>(Func<T, T2?>)
Returns the result of the provided ok
delegate invocation when IsOk is equal to true,
otherwise returns None.
[Pure]
public Maybe<T2> IfOk<T2>(Func<T, T2?> ok) where T2 : notnull
Parameters
Returns
Type Parameters
T2
Result type.
Match(Action<T>, Action<Exception>)
Invokes the provided ok
delegate when IsOk is equal to true,
otherwise invokes the provided error
delegate.
public Nil Match(Action<T> ok, Action<Exception> error)
Parameters
ok
Action<T>Delegate to invoke when IsOk is equal to true.
error
Action<Exception>Delegate to invoke when IsOk is equal to false.
Returns
Match<T2>(Func<T, T2>, Func<Exception, T2>)
Returns the result of the provided ok
delegate invocation when IsOk is equal to true,
otherwise returns the result of the provided error
delegate invocation.
[Pure]
public T2 Match<T2>(Func<T, T2> ok, Func<Exception, T2> error)
Parameters
ok
Func<T, T2>Delegate to invoke when IsOk is equal to true.
error
Func<Exception, T2>Delegate to invoke when IsOk is equal to false.
Returns
- T2
Delegate invocation result.
Type Parameters
T2
Result type.
ToString()
Returns a string representation of this Erratic<T> instance.
[Pure]
public override string ToString()
Returns
- string
String representation.
Operators
operator ==(Erratic<T>, Erratic<T>)
Checks if a
is equal to b
.
[Pure]
public static bool operator ==(Erratic<T> a, Erratic<T> b)
Parameters
Returns
- bool
true when operands are equal, otherwise false.
explicit operator Exception(Erratic<T>)
Gets the underlying error.
[Pure]
public static explicit operator Exception(Erratic<T> value)
Parameters
value
Erratic<T>
Returns
- Exception
Underlying error.
Exceptions
- ValueAccessException
When underlying error does not exist.
explicit operator T(Erratic<T>)
Gets the underlying value.
[Pure]
public static explicit operator T(Erratic<T> value)
Parameters
value
Erratic<T>
Returns
- T
Underlying value.
Exceptions
- ValueAccessException
When underlying value does not exist.
implicit operator Erratic<T>(Either<T, Exception>)
Converts the provided value
to an equivalent Erratic<T> instance.
[Pure]
public static implicit operator Erratic<T>(Either<T, Exception> value)
Parameters
Returns
- Erratic<T>
New Erratic<T> instance.
implicit operator Either<T, Exception>(Erratic<T>)
Converts the provided value
to an equivalent Either<T1, T2> instance.
[Pure]
public static implicit operator Either<T, Exception>(Erratic<T> value)
Parameters
value
Erratic<T>Value to convert.
Returns
- Either<T, Exception>
New Either<T1, T2> instance.
implicit operator Erratic<T>(Nil)
Converts Nil to an Erratic<T> instance.
[Pure]
public static implicit operator Erratic<T>(Nil value)
Parameters
value
NilValue to convert.
Returns
implicit operator Erratic<T>(Exception)
Converts the provided error
to an Erratic<T> instance with an error.
[Pure]
public static implicit operator Erratic<T>(Exception error)
Parameters
error
ExceptionException to convert.
Returns
- Erratic<T>
New Erratic<T> instance.
implicit operator Erratic<T>(T)
Converts the provided value
to an Erratic<T> instance without an error.
[Pure]
public static implicit operator Erratic<T>(T value)
Parameters
value
TValue to convert.
Returns
- Erratic<T>
New Erratic<T> instance.
operator !=(Erratic<T>, Erratic<T>)
Checks if a
is not equal to b
.
[Pure]
public static bool operator !=(Erratic<T> a, Erratic<T> b)
Parameters
Returns
- bool
true when operands are not equal, otherwise false.