F#:柯里化重载/元组重载问题

发布于 2024-07-22 23:58:13 字数 674 浏览 4 评论 0原文

在将一些代码迁移到最新版本的 F#(包含在 VS2010 b1 中)时,我遇到了一个问题,我想知道是否有可用的解决方法,如果没有,为什么 F# 编译器的行为不修改为支持场景。


type Foo(a) =
    [<OverloadID("CurriedAbc")>]
    member public x.Abc (p:(oneType * anotherType) seq) otherParm = method impl...

    //this overload exists for better compatibility with other languages
    [<OverloadID("TupledAbc")>]
    member public x.Abc (p:Dictionary<oneType, anotherType>, otherParm) =
        x.Abc(p |> Seq.map(fun kvp -> (kvp.Key, kvp.Value))) otherParm

此代码会产生以下编译时错误:

错误 FS0191:此方法的一个或多个重载具有柯里化参数。 考虑重新设计这些成员以采用元组形式的参数

请注意,这曾经在 F# 1.9.6.2(9 月 CTP)上完美运行

While migrating some code to the latest version of F#, included in VS2010 b1, I've encountered an issue and I'd like to know if there's a workaround available and - if not - why was the behavior of the F# compiler modified not to support the scenario.


type Foo(a) =
    [<OverloadID("CurriedAbc")>]
    member public x.Abc (p:(oneType * anotherType) seq) otherParm = method impl...

    //this overload exists for better compatibility with other languages
    [<OverloadID("TupledAbc")>]
    member public x.Abc (p:Dictionary<oneType, anotherType>, otherParm) =
        x.Abc(p |> Seq.map(fun kvp -> (kvp.Key, kvp.Value))) otherParm

This code produces the following compile-time error:

error FS0191: One or more of the overloads of this method has curried arguments. Consider redesigning these members to take arguments in tupled form

Please mind that this used to work flawlessly on F# 1.9.6.2 (Sept. CTP)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杯别 2024-07-29 23:58:13

更改的原因位于 详细发行说明

柯里化方法的优化

柯里化成员如下所示:

输入 C() =

静态成员 Sum ab = a + b     
  

在之前的 F# 实现中
柯里化成员编译较少
比非柯里化成员更有效。
现在这一点已经改变。 然而,
现在有一些小限制
关于柯里化成员的定义:

  • 柯里化成员不得重载
  • 可能需要调整柯里化成员的某些定义,以向定义中添加正确数量的参数

由于只能在第一个参数上解析重载,因此您应该能够通过将柯里化版本更改为:

    [<OverloadID("CurriedAbc")>]
    member public x.Abc (p:(oneType * anotherType) seq)
       = fun otherParm -> method impl...

The reason for the change is in the detailed release notes:

Optimizations for Curried Methods

A curried member looks like this:

type C() =

static member Sum a b = a + b    

In previous implementations of F#
curried members were compiled less
efficiently than non-curried members.
This has now been changed. However,
there are now some small restrictions
on the definition of curried members:

  • curried members may not be overloaded
  • some definitions of curried members may need to be adjusted to add the correct number of parameters to the definition

Since your overloading can be resolved only on the first parameter, you should be able to work round it by changing the curried version to:

    [<OverloadID("CurriedAbc")>]
    member public x.Abc (p:(oneType * anotherType) seq)
       = fun otherParm -> method impl...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文