我认为F#函数和System.Func之间的转换必须手动完成,但似乎有编译器(有时)会为您执行它的情况。而当它出现问题的错误消息是不准确的:代表/ Func转换和误导性编译器错误消息
module Foo =
let dict = new System.Collections.Generic.Dictionary<string, System.Func<obj,obj>>()
let f (x:obj) = x
do
// Question 1: why does this compile without explicit type conversion?
dict.["foo"] <- fun (x:obj) -> x
// Question 2: given that the above line compiles, why does this fail?
dict.["bar"] <- f
最后一行编译失败,错误是:
This expression was expected to have type
System.Func<obj,obj>
but here has type
'a -> obj
显然,功能f
不具有签名'a > obj
。如果F#3.1编译器对第一个字典赋值感到满意,那为什么不是第二个呢?
我想这是一个转换不是自动的情况,但这个错误并不是很有帮助,但可能在技术上仍然是正确的。 –
我猜想只有lambda会自动转换。 – MisterMetaphor
@MisterMetaphor:correct:http://stackoverflow.com/questions/3392000/interop-between-f-and-c-sharp-lambdas – Mau