Struct TypeCast<TSource, TDestination>
- Namespace
- LfrlAnvil.Functional
- Assembly
- LfrlAnvil.Functional.dll
Represents a generic result of a type cast.
public readonly struct TypeCast<TSource, TDestination> : ITypeCast<TDestination>, IReadOnlyCollection<TDestination>, IEnumerable<TDestination>, IEnumerable, IEquatable<TypeCast<TSource, TDestination>>
Type Parameters
TSource
Source type.
TDestination
Destination type.
- Implements
-
ITypeCast<TDestination>IReadOnlyCollection<TDestination>IEnumerable<TDestination>IEquatable<TypeCast<TSource, TDestination>>
- Inherited Members
- Extension Methods
Fields
Empty
Represents an invalid type cast.
public static readonly TypeCast<TSource, TDestination> Empty
Field Value
- TypeCast<TSource, TDestination>
Properties
IsInvalid
Specifies whether or not this type cast is invalid.
public bool IsInvalid { get; }
Property Value
IsValid
Specifies whether or not this type cast is valid.
public bool IsValid { get; }
Property Value
Source
Underlying source object.
public TSource Source { get; }
Property Value
- TSource
Methods
Bind<T>(Func<TDestination, TypeCast<TDestination, T>>)
Returns the result of the provided valid
delegate invocation when IsValid is equal to true,
otherwise returns this instance.
[Pure]
public TypeCast<TDestination, T> Bind<T>(Func<TDestination, TypeCast<TDestination, T>> valid)
Parameters
valid
Func<TDestination, TypeCast<TDestination, T>>Delegate to invoke when IsValid is equal to true.
Returns
- TypeCast<TDestination, T>
New TypeCast<TSource, TDestination> instance.
Type Parameters
T
Result type.
Bind<T>(Func<TDestination, TypeCast<TDestination, T>>, Func<TSource, TypeCast<TDestination, T>>)
Returns the result of the provided valid
delegate invocation when IsValid is equal to true,
otherwise returns the result of the provided invalid
delegate invocation.
[Pure]
public TypeCast<TDestination, T> Bind<T>(Func<TDestination, TypeCast<TDestination, T>> valid, Func<TSource, TypeCast<TDestination, T>> invalid)
Parameters
valid
Func<TDestination, TypeCast<TDestination, T>>Delegate to invoke when IsValid is equal to true.
invalid
Func<TSource, TypeCast<TDestination, T>>Delegate to invoke when IsValid is equal to false.
Returns
- TypeCast<TDestination, T>
New TypeCast<TSource, TDestination> instance.
Type Parameters
T
Delegate's result type.
Equals(TypeCast<TSource, TDestination>)
Indicates whether the current object is equal to another object of the same type.
[Pure]
public bool Equals(TypeCast<TSource, TDestination> other)
Parameters
other
TypeCast<TSource, TDestination>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.
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.
GetResult()
Gets the underlying type cast result.
[Pure]
public TDestination GetResult()
Returns
- TDestination
Underlying type cast result.
Exceptions
- ValueAccessException
When underlying type cast result does not exist.
GetResultOrDefault()
Gets the underlying type cast result or a default value when it does not exist.
[Pure]
public TDestination? GetResultOrDefault()
Returns
- TDestination
Underlying type cast result or a default value when it does not exist.
GetResultOrDefault(TDestination)
Gets the underlying type cast result or a default value when it does not exist.
[Pure]
public TDestination GetResultOrDefault(TDestination defaultValue)
Parameters
defaultValue
TDestinationDefault value to return in case the type cast result does not exist.
Returns
- TDestination
Underlying type cast result or a default value when it does not exist.
IfInvalid(Action<TSource>)
Invokes the provided invalid
delegate when IsInvalid is equal to true,
otherwise does nothing.
public Nil IfInvalid(Action<TSource> invalid)
Parameters
Returns
IfInvalidOrDefault<T>(Func<TSource, T>)
Returns the result of the provided invalid
delegate invocation when IsInvalid
is equal to true, otherwise returns a default value.
[Pure]
public T? IfInvalidOrDefault<T>(Func<TSource, T> invalid)
Parameters
Returns
- T
Delegate invocation result or a default value.
Type Parameters
T
Result type.
IfInvalidOrDefault<T>(Func<TSource, T>, T)
Returns the result of the provided invalid
delegate invocation when IsInvalid
is equal to true, otherwise returns a default value.
[Pure]
public T IfInvalidOrDefault<T>(Func<TSource, T> invalid, T defaultValue)
Parameters
invalid
Func<TSource, T>Delegate to invoke when IsInvalid is equal to true.
defaultValue
TValue to return IsInvalid is equal to false.
Returns
- T
Delegate invocation result or a default value.
Type Parameters
T
Result type.
IfInvalid<T>(Func<TSource, T?>)
Returns the result of the provided invalid
delegate invocation when IsInvalid
is equal to true, otherwise returns None.
[Pure]
public Maybe<T> IfInvalid<T>(Func<TSource, T?> invalid) where T : notnull
Parameters
Returns
Type Parameters
T
Result type.
IfValid(Action<TDestination>)
Invokes the provided valid
delegate when IsValid is equal to true,
otherwise does nothing.
public Nil IfValid(Action<TDestination> valid)
Parameters
Returns
IfValidOrDefault<T>(Func<TDestination, T>)
Returns the result of the provided valid
delegate invocation when IsValid
is equal to true, otherwise returns a default value.
[Pure]
public T? IfValidOrDefault<T>(Func<TDestination, T> valid)
Parameters
Returns
- T
Delegate invocation result or a default value.
Type Parameters
T
Result type.
IfValidOrDefault<T>(Func<TDestination, T>, T)
Returns the result of the provided valid
delegate invocation when IsValid
is equal to true, otherwise returns a default value.
[Pure]
public T IfValidOrDefault<T>(Func<TDestination, T> valid, T defaultValue)
Parameters
valid
Func<TDestination, T>Delegate to invoke when IsValid is equal to true.
defaultValue
TValue to return IsValid is equal to false.
Returns
- T
Delegate invocation result or a default value.
Type Parameters
T
Result type.
IfValid<T>(Func<TDestination, T?>)
Returns the result of the provided valid
delegate invocation when IsValid
is equal to true, otherwise returns None.
[Pure]
public Maybe<T> IfValid<T>(Func<TDestination, T?> valid) where T : notnull
Parameters
Returns
Type Parameters
T
Result type.
Match(Action<TDestination>, Action<TSource>)
Invokes the provided valid
delegate when IsValid is equal to true,
otherwise invokes the provided invalid
delegate.
public Nil Match(Action<TDestination> valid, Action<TSource> invalid)
Parameters
valid
Action<TDestination>Delegate to invoke when IsValid is equal to true.
invalid
Action<TSource>Delegate to invoke when IsValid is equal to false.
Returns
Match<T>(Func<TDestination, T>, Func<TSource, T>)
Returns the result of the provided valid
delegate invocation when IsValid is equal to true,
otherwise returns the result of the provided invalid
delegate invocation.
[Pure]
public T Match<T>(Func<TDestination, T> valid, Func<TSource, T> invalid)
Parameters
valid
Func<TDestination, T>Delegate to invoke when IsValid is equal to true.
invalid
Func<TSource, T>Delegate to invoke when IsValid is equal to false.
Returns
- T
Delegate invocation result.
Type Parameters
T
Result type.
ToString()
Returns a string representation of this TypeCast<TSource, TDestination> instance.
[Pure]
public override string ToString()
Returns
- string
String representation.
Operators
operator ==(TypeCast<TSource, TDestination>, TypeCast<TSource, TDestination>)
Checks if a
is equal to b
.
[Pure]
public static bool operator ==(TypeCast<TSource, TDestination> a, TypeCast<TSource, TDestination> b)
Parameters
Returns
- bool
true when operands are equal, otherwise false.
explicit operator TDestination(TypeCast<TSource, TDestination>)
Gets the underlying type cast result.
[Pure]
public static explicit operator TDestination(TypeCast<TSource, TDestination> value)
Parameters
value
TypeCast<TSource, TDestination>
Returns
- TDestination
Underlying type cast result.
Exceptions
- ValueAccessException
When underlying type cast result does not exist.
implicit operator TypeCast<TSource, TDestination>(Nil)
Converts Nil to an TypeCast<TSource, TDestination> instance.
[Pure]
public static implicit operator TypeCast<TSource, TDestination>(Nil value)
Parameters
value
NilValue to convert.
Returns
implicit operator TypeCast<TSource, TDestination>(PartialTypeCast<TSource>)
Converts the provided value
to a TypeCast<TSource, TDestination> instance.
[Pure]
public static implicit operator TypeCast<TSource, TDestination>(PartialTypeCast<TSource> value)
Parameters
value
PartialTypeCast<TSource>Value to convert.
Returns
- TypeCast<TSource, TDestination>
New TypeCast<TSource, TDestination> instance.
implicit operator TypeCast<TSource, TDestination>(TSource)
Converts the provided value
to a TypeCast<TSource, TDestination> instance.
[Pure]
public static implicit operator TypeCast<TSource, TDestination>(TSource value)
Parameters
value
TSourceValue to convert.
Returns
- TypeCast<TSource, TDestination>
New TypeCast<TSource, TDestination> instance.
operator !=(TypeCast<TSource, TDestination>, TypeCast<TSource, TDestination>)
Checks if a
is not equal to b
.
[Pure]
public static bool operator !=(TypeCast<TSource, TDestination> a, TypeCast<TSource, TDestination> b)
Parameters
Returns
- bool
true when operands are not equal, otherwise false.