首页 \ 问答 \ F#lambda与Func的重载分辨率(overload resolution of F# lambda vs Func)

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
更新时间:2024-01-27 07:01

最满意答案

  1. div标签中删除ng-app="myApp"
  2. 单击HTML右上角的齿轮图标,然后在BODY TAG字段中输入<body ng-app="myApp">
  3. 单击JavaScript右上角的齿轮图标,然后在LOAD TYPE中选择No wrap - in <body>
  4. 单击更新然后运行。
  5. 利润!

  1. Remove ng-app="myApp" from the div tag.
  2. Click the gear icon in the top right corner of HTML and in the BODY TAG field enter <body ng-app="myApp">.
  3. Click the gear icon in the top right corner of JavaScript and in the LOAD TYPE select No wrap - in <body>
  4. Click Update then Run.
  5. Profit!

相关问答

更多
  • 从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 ...
  • 好的,这就是我解决它的方式。 如果我将stoppropagation函数放在另一个函数中,然后从可编辑div中的onclick事件调用该函数,那么一切正常