特定泛型类型的扩展方法
我正在尝试为 F# 中绑定到特定泛型类型参数的泛型类型创建各种扩展方法,但语言似乎不允许我这样做:
我想做的是如下所示:
type IEnumerable<int> with
member this.foo =
this.ToString()
然而它给了我编译器错误(在 int
关键字下划线):
类型名称中存在意外的标识符。需要中缀运算符、引号或其他标记。
下面的确实有效,尽管它没有像我想要的那样将泛型类型参数专门绑定到int
:
type IEnumerable<'a> with
member this.foo =
this.ToString()
有没有办法在F#中实现这个目标 - 我也许只是使用了错误的语法?如果没有,如果有人可以建议一种解决方法,也许在某处使用类型约束,我将不胜感激。
I'm attempting to create various extension method for a generic type bound to specific generic type parameters in F#, but the language does not seem to be allowing me:
What I want to do is something like the following:
type IEnumerable<int> with
member this.foo =
this.ToString()
Yet it gives me the compiler error (underlining the int
keyword):
Unexpected identifier in type name. Expected infix operator, quote symbol or other token.
The following does work, though it does not specifically bind the generic type parameter to int
, as I want:
type IEnumerable<'a> with
member this.foo =
this.ToString()
Is there any way to accomplish this aim in F# - am I perhaps just using the wrong syntax? If not, I would appreciate if someone could suggest a workaround, perhaps using type constraints somewhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
F# 3.1 中现在提供了通用扩展方法:
Generic extension methods are now available in F# 3.1:
不幸的是,这在当前版本的 F# 中是不可能的。请参阅此处相关问题。
This isn't possible in the current version of F#, unfortunately. See related question here.
好吧,您可以使用约束 - 但不能使用 int 等密封类型。
唔...
Well, you can use constraints - but not with sealed types like int.
Hmm...
为了帮助其他人寻找类似的解决方案,这里有一个示例,展示如何使用带有类型约束的通用扩展方法。在下面的示例中,有一个类型约束,要求传递的类型参数公开默认构造函数。这是使用应用于
Order
记录的[]
属性来完成的。另外,我将该方法的结果限制为传递的类型。为了使用扩展方法,您必须指定要使用的类型。请注意,我还扩展了通用字典接口。
In order to help others looking for similar solutions, here is an example showing how to use generic extension methods with type constraints. In the example below, there is a type constraint requiring that the type argument passed exposes a default constructor. This is done using the
[<CLIMutable>]
attribute applied to theOrder
record. Also, I'm constraing the result of the method to the type passed.In order to use the extension method you have to specify the type you want to use. Note that I'm also extending a generic dictionary interface.