我可以想象这将是多个重载的问题,但是(除了Linq),大部分代码可能只有一个重载。
只有一个过载时,可以节省额外的铸造样板,避免出现Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type.
之类的错误。
当然,您可能会争辩说,在以后的阶段中添加重载时,代码将即再次开始触发上述编译器错误。但话又说回来-添加重载也可能破坏类型化的行为(即具有多个接口)。
这是否只是暂时的优先事项?还是我缺少一些概念上的问题?我要这是为了进一步了解语言设计。
static int MapLolCats(int amountOfLolcats, Func<dynamic,int> mappingFunction)
{
return mappingFunction(amountOfLolcats);
}
// This works as expected
var amountOfLegs = MapLolCats(5, x => x * 4);
// This creates a compilation error (despite you could deduct what is meant, because there is only one overload)
// Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
dynamic jsonValue = 5;
var amountOfLegs = MapLolCats(jsonValue, x => x * 4);