简体   繁体   English

为什么我只能在成员方法中将F#函数传递给System.Func参数?

[英]Why can I pass an F# function to a System.Func parameter only in a member method?

This works: 这有效:

type MyType () =
    static member MyFn (fn : Func<bool>) = fn.Invoke ()

MyType.MyFn (fun _ -> false)

This does not (error FS0002): 这不(错误FS0002):

let myFn (fn : Func<bool>) = fn.Invoke ()

myFn (fun _ -> false)

Neither does this (error FS0002): 这也不是(错误FS0002):

type MyDU = Fn of Func<bool>

Fn (fun _ -> false)

What is the reason for this rather annoying inconsistency? 这种相当恼人的不一致的原因是什么?

This behavior is covered by section 8.13.7 (Type-directed Conversions at Member Invocations) of the F# spec , where it states: F#规范的第8.13.7节(成员调用中的类型导向转换)涵盖了此行为,其中指出:

Note: These type-directed conversions are primarily for interoperability with existing member-based .NET libraries and do not apply at invocations of functions defined in modules or bound locally in expressions. 注意:这些类型定向转换主要用于与现有基于成员的.NET库的互操作性,不适用于在模块中定义的函数的调用或在表达式中本地绑定的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM