也许是类和可选参数

bra*_*ing 2 c# optional-parameters compile-time-constant maybe

我在c#中实现了一个Maybe/Option类.基本实现是

public delegate Maybe<TOutput> Converter<in TInput, out TOutput>(TInput input);
public delegate TOutput ElseDelegate<out TOutput>();
public delegate Maybe<TOutput> ElseDelegate2<out TOutput>();

public interface Maybe<out TResult> : IEnumerable<TResult>
{
    Maybe<B> Bind<B>(Converter<TResult, B> f);
    TResult Value();
    bool IsSome();
}

public static class Maybe
{
    public static Maybe<T> None<T>()
    {
        return new None<T>();
    }
}

public interface INone<out TResult> : Maybe<TResult>
{
}

public interface ISome<out TResult> : Maybe<TResult>
{
}

public struct None<TResult> : INone<TResult>
{

    public IEnumerator<TResult> GetEnumerator()
    { yield break; }

    IEnumerator IEnumerable.GetEnumerator()
    { yield break; }


    public bool IsSome() { return false; }

    public Maybe<TOutput> Bind<TOutput>(Converter<TResult, TOutput> f)
    {
        return new None<TOutput>();
    }

    public TResult Value()
    {
        throw new IndexOutOfRangeException("None has no value");
    }
}

public struct Some<TResult> : Maybe<TResult>
{
    private TResult _Value;
    public Some(TResult value)
    {
        _Value = value;
    }

    public IEnumerator<TResult> GetEnumerator()
    { yield return _Value; }

    IEnumerator IEnumerable.GetEnumerator()
    { yield return _Value; }

    public bool IsSome() { return true; }

    public Maybe<TOutput> Bind<TOutput>(Converter<TResult, TOutput> f)
    {
        return f(_Value);
    }

    public TResult Value()
    {
        return this._Value;
    }
}
#endregion
Run Code Online (Sandbox Code Playgroud)

有一堆扩展方法,我没有包括在这里.一切正常.但是我想要实现的标准模式如下,使用Maybe实现可选参数默认值,如F#

void DoSomeCalc
    ( Maybe<double> x = Maybe.None<double>()
    , Maybe<double> y = Maybe.None<double>()
    )
{
    this.X = x.Else( ()=> CalculateDefaultX() );
    this.Y = y.Else( ()=> CalculateDefaultY() );
}
Run Code Online (Sandbox Code Playgroud)

所以我能做到

DoSomeCalc(x:10)
Run Code Online (Sandbox Code Playgroud)

要么

DoSomeCalc(y:20)
Run Code Online (Sandbox Code Playgroud)

如果无可用,则Else提供值.然而,这在理论上都很好,但C#可选参数必须是编译时间常量,这完全搞砸了这种模式.

任何人都可以提出一个修复程序来保持模式的意图而不会在这里引入nullables或null吗?

无论如何,我可以创建一个编译时常量来表示None,这将与我上面的Maybe实现一起使用吗?

Jon*_*eet 6

不,你在这里无能为力.您的参数类型是引用类型,这意味着唯一可用的常量值是null字符串文字.(显然字符串文字在你的情况下没用;我只提到它们是唯一一种非空引用类型常量.)

一种选择是创建Maybe<T>结构而不是接口,默认值为"none"值.这将基本上相同Nullable<T>但没有T必须是非可空值类型的约束.然后你可以使用:

void DoSomeCalc(Maybe<double> x = default(Maybe<double>),
                Maybe<double> y = default(Maybe<double>))
Run Code Online (Sandbox Code Playgroud)

显示所有这些的示例代码:

using System;

struct Maybe<T>
{
    private readonly bool hasValue;
    public bool HasValue { get { return hasValue; } }

    private readonly T value;
    public T Value
    {
        get
        {
            if (!hasValue)
            {
                throw new InvalidOperationException();
            }
            return value;
        }
    }

    public Maybe(T value)
    {
        this.hasValue = true;
        this.value = value;
    }

    public static implicit operator Maybe<T>(T value)
    {
        return new Maybe<T>(value);
    }
}

class Test
{
    static void DoSomeCalc(Maybe<double> x = default(Maybe<double>),
                           Maybe<double> y = default(Maybe<double>))
    {
        Console.WriteLine(x.HasValue ? "x = " + x.Value : "No x");
        Console.WriteLine(y.HasValue ? "y = " + y.Value : "No y");
    }

    static void Main()
    {
        Console.WriteLine("First call");
        DoSomeCalc(x: 10);

        Console.WriteLine("Second call");
        DoSomeCalc(y: 20);
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,你想要添加更多功能Maybe<T>,例如覆盖ToStringEquals,但你会得到一般的想法.当然,您仍然可以Maybe使用工厂方法创建非泛型类.