Table of Contents

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

bool

IsValid

Specifies whether or not this type cast is valid.

public bool IsValid { get; }

Property Value

bool

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

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

[Pure]
public override bool Equals(object? obj)

Parameters

obj object

The 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 TDestination

Default 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

invalid Action<TSource>

Delegate to invoke when IsInvalid is equal to true.

Returns

Nil

Nil.

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

invalid Func<TSource, T>

Delegate to invoke when IsInvalid is equal to true.

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 T

Value 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

invalid Func<TSource, T>

Delegate to invoke when IsInvalid is equal to true.

Returns

Maybe<T>

New Maybe<T> instance.

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

valid Action<TDestination>

Delegate to invoke when IsValid is equal to true.

Returns

Nil

Nil.

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

valid Func<TDestination, T>

Delegate to invoke when IsValid is equal to true.

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 T

Value 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

valid Func<TDestination, T>

Delegate to invoke when IsValid is equal to true.

Returns

Maybe<T>

New Maybe<T> instance.

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

Nil

Nil.

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

a TypeCast<TSource, TDestination>

First operand.

b TypeCast<TSource, TDestination>

Second operand.

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 Nil

Value to convert.

Returns

TypeCast<TSource, TDestination>

Empty.

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 TSource

Value 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

a TypeCast<TSource, TDestination>

First operand.

b TypeCast<TSource, TDestination>

Second operand.

Returns

bool

true when operands are not equal, otherwise false.