F#lambda与Func的重载分辨率(overload resolution of F# lambda vs Func)
我将静态构建器方法添加到像这样的记录类型:
type ThingConfig = { url: string; token : string; } with static member FromSettings (getSetting : (string -> string)) : ThingConfig = { url = getSetting "apiUrl"; token = getSetting "apiToken"; }
我可以这样称呼它:
let config = ThingConfig.FromSettings mySettingsAccessor
现在,棘手的部分:我想添加第二个重载的构建器从C#中使用(现在忽略重复的实现):
static member FromSettings (getSetting : System.Func<string,string>) : ThingConfig = { url = getSetting.Invoke "apiUrl"; token = getSetting.Invoke "apiToken"; }
这适用于C#,但打破了我以前的F#调用错误FS0041:无法根据此程序点之前的类型信息确定方法'FromSettings'的唯一重载。 可能需要类型注释。 候选人:静态成员ThingConfig.FromSettings:getSetting:(字符串 - >字符串) - > ThingConfig,静态成员ThingConfig.FromSettings:getSetting:Func - > ThingConfig
为什么F#不知道要调用哪一个?
那种类型的注释是什么样的? (我可以注释来自呼叫站点的参数类型吗?)
这种互操作模式有没有更好的模式? (重载接受来自C#和F#的lambdas)
I'm adding a static builder method to a record type like this:
type ThingConfig = { url: string; token : string; } with static member FromSettings (getSetting : (string -> string)) : ThingConfig = { url = getSetting "apiUrl"; token = getSetting "apiToken"; }
I can call it like this:
let config = ThingConfig.FromSettings mySettingsAccessor
Now the tricky part: I'd like to add a second overloaded builder for use from C# (ignore the duplicated implementation for now):
static member FromSettings (getSetting : System.Func<string,string>) : ThingConfig = { url = getSetting.Invoke "apiUrl"; token = getSetting.Invoke "apiToken"; }
This works for C#, but breaks my earlier F# call with error FS0041: A unique overload for method 'FromSettings' could not be determined based on type information prior to this program point. A type annotation may be needed. Candidates: static member ThingConfig.FromSettings : getSetting:(string -> string) -> ThingConfig, static member ThingConfig.FromSettings : getSetting:Func -> ThingConfig
Why can't F# figure out which one to call?
What would that type annotation look like? (Can I annotate the parameter type from the call site?)
Is there a better pattern for this kind of interop? (overloads accepting lambdas from both C# and F#)
原文:https://stackoverflow.com/questions/49164915
最满意答案
- 从
div
标签中删除ng-app="myApp"
。- 单击HTML右上角的齿轮图标,然后在BODY TAG字段中输入
<body ng-app="myApp">
。- 单击JavaScript右上角的齿轮图标,然后在LOAD TYPE中选择
No wrap - in <body>
- 单击更新然后运行。
- 利润!
- Remove
ng-app="myApp"
from thediv
tag.- Click the gear icon in the top right corner of HTML and in the BODY TAG field enter
<body ng-app="myApp">
.- Click the gear icon in the top right corner of JavaScript and in the LOAD TYPE select
No wrap - in <body>
- Click Update then Run.
- Profit!
相关问答
更多-
谁能告诉我为什么这个简单的angularjs jsfiddle无法正常工作?(Can someone tell me why this simple angularjs jsfiddle not working?)[2023-02-10]
从div标签中删除ng-app="myApp" 。 单击HTML右上角的齿轮图标,然后在BODY TAG字段中输入 。 单击JavaScript右上角的齿轮图标,然后在LOAD TYPE中选择No wrap - in 单击更新然后运行。 利润! Remove ng-app="myApp" from the div tag. Click the gear icon in the top right corner of HTML and in the BO ... -
jQuery draggable在jsfiddle中工作但不在webserver上工作(jQuery draggable working in jsfiddle but not on webserver)[2021-10-11]
好的,这就是我解决它的方式。 如果我将stoppropagation函数放在另一个函数中,然后从可编辑div中的onclick事件调用该函数,那么一切正常