Cunc中的C#歧义+扩展方法+ lambdas

时间:2010-04-24 05:04:03

标签: c# generics extension-methods monads func

我一直试图通过这篇文章:

http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx

......第1页上的内容让我感到不舒服。特别是,我试图围绕Compose<>()函数,并为自己写了一个例子。考虑以下两个Func:

Func<double, double> addTenth = x => x + 0.10;
Func<double, string> toPercentString = x => (x * 100.0).ToString() + "%";

没问题!很容易理解这两者的作用。

现在,按照本文中的示例,您可以编写一个通用的扩展方法来组合这些函数,如下所示:

public static class ExtensionMethods
{
 public static Func<TInput, TLastOutput> Compose<TInput, TFirstOutput, TLastOutput>(
  this Func<TFirstOutput, TLastOutput> toPercentString,
  Func<TInput, TFirstOutput> addTenth)
 {
  return input => toPercentString(addTenth(input));
 }
}

精细。所以现在你可以说:

string x = toPercentString.Compose<double, double, string>(addTenth)(0.4);

你得到字符串“50%”

到目前为止,非常好。

但这里有一些含糊不清的东西。假设您编写了另一种扩展方法,现在您有两个函数:

public static class ExtensionMethods
{
 public static Func<TInput, TLastOutput> Compose<TInput, TFirstOutput, TLastOutput>(
  this Func<TFirstOutput, TLastOutput> toPercentString,
  Func<TInput, TFirstOutput> addTenth)
 {
  return input => toPercentString(addTenth(input));
 }

 public static Func<double, string> Compose<TInput, TFirstOutput, TLastOutput>(this
  Func<double, string> toPercentString,
  Func<double, double> addTenth)
 {
  return input => toPercentString(addTenth(input + 99999));
 }
}

这里有歧义。这两个功能不具有重叠签名吗?是。这甚至可以编译吗?是。哪一个被称为?第二个(显然会给你“错误的”结果)被调用。如果你注释掉任何一个函数,它仍会编译,但你会得到不同的结果。

这似乎是挑剔,但有些东西在这里深深地冒犯了我的感情,我不能把手指放在上面。它与扩展方法有关吗?它与lambdas有关吗?或者它与Func&lt;&gt;的关系有什么关系?允许您参数化返回类型?我不确定。

我猜这是在规范的某处解决的,但我甚至不知道Google会发现什么。

帮助!

2 个答案:

答案 0 :(得分:6)

这里没有任何含糊之处。第二个将在完全匹配时被调用。每当匹配不准确时,你会得到第一个函数,因为默认情况下它将与其他所有函数完全匹配。

如果您创建Func<double, string>,另一个Func<double, double>,则在显式指定<double, double, string>时调用.Compose,编译器有足够的信息来确定第二个版本将是完全匹配,因此它是使用的。

但请考虑这个愚蠢的例子:

Func<string, string> doubleString = s => s + s;
Func<DateTime, string> dateToString = date => date.ToString();

Func<DateTime, string> composedFunction = doubleString.Compose(dateToString);
Console.WriteLine(composedFunction(DateTime.Now));

调用哪个版本?结果是什么?第一个版本,输出是作为连接到自身的字符串的日期。

另一方面,如果你有一个更实际的例子使用Func<double, string>Func<double, double>并且对Compose的调用不那么明确,那么调用哪个版本?

Func<double, string> toPercentString = d => d.ToString("0.0%");
Func<double, double> addTenth = d => d + 0.1;
Console.WriteLine(toPercentString.Compose(addTenth)(0.8));

第一个,因为编译器确定它与第二个完全匹配。由于我不是Eric Lippert或Jon Skeet,我甚至都不会尝试解释那个绑定。


static void DoSomething(float f, double d) { }
static void DoSomething(double d, float f) { }

...

DoSomething(1, 1);

这个是不明确的(并且因为它而无法编译)。

答案 1 :(得分:0)

我不知道这与委托,泛型或扩展方法有什么关系;你的基本问题似乎是重载(特别是当有多个候选者时方法重载解决方案)。考虑:

public static class Test {
    public static void Method(string s) { 
        Console.WriteLine("String version: " + s); 
    }
    public static void Method(object o) { 
        Console.WriteLine("Object version: " + o.ToString()); 
    }

    public static void Main(string[] args) { Method("my string"); }

}

Main中,调用哪个方法?有歧义吗?在编译期间,重载分辨率会被踢出,并且确定一种方法更合适(采用字符串的方法)。就像你的情况一样,注释掉第一个重载将导致代码编译但调用另一个重载。如果将第二种方法定义为:

,则会发生同样的情况
    public static void Method<T>(T t) { 
        Console.WriteLine("Generic version: " + t.ToString()); 
    }

虽然这是重载解析的候选者,但是使用字符串的重载是完全匹配的,并且是首选。