PutridParrot.Options/OptionExtensions.cs
/// <summary>
/// SafeCast is the same as a standard cast but no exception occurs, instead
/// a None would be returned for a cast failure.
/// </summary>
/// <typeparam name="TFrom"></typeparam>
/// <typeparam name="TTo"></typeparam>
/// <param name="option"></param>
/// <returns></returns>
SafeCast is the same as a standard cast but no exception occurs, instead
a None would be returned for a cast failure.
SafeCast is the same as a standard cast but no exception occurs instead a None would be returned for a cast failure .
public static Option<TTo> SafeCast<TFrom, TTo>(this Option<TFrom> option)
            where TTo : class
        {
            switch (option)
            {
                case Some<TFrom> some:
                    var cast = some.Value as TTo;
                    if (cast == null)
                    {
                        return Option<TTo>.None;
                    }
                    return cast.ToOption();
                default:
                    return Option<TTo>.None;
            }
        }
public static Option < TTo > SafeCast < TFrom , TTo > ( this Option < TFrom > option ) where TTo : class { switch ( option ) { case Some < TFrom > some : var cast = some . Value as TTo ; if ( cast == null ) { return Option < TTo > . None ; } return cast . ToOption ( ) ; default : return Option < TTo > . None ; } }
SafeCast is the same as a standard cast but no exception occurs, instead
a None would be returned for a cast failure.
SafeCast is the same as a standard cast but no exception occurs instead a None would be returned for a cast failure .
None
Option<TFrom>
this
option
None
typeparam
None
typeparam
None