特定通用类型的扩展方法 [英] Extension methods for specific generic types

查看:104
本文介绍了特定通用类型的扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在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()

然而,给出了编译器错误(在 int 关键字下面加上下划线):

Yet it gives me the compiler error (underlining the int keyword):


类型名称。

Unexpected identifier in type name. Expected infix operator, quote symbol or other token.

以下确实可以起作用,虽然它并没有特别指出将泛型类型参数绑定到 int ,如我所愿:

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()

任何方式来完成这个目标在F# - 我可能只是使用错误的语法?如果没有,我会很感激,如果有人可以建议一种解决方法,也许在某处使用类型约束。

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.

推荐答案

这是不可能的不幸的是,当前版本的F#。请参阅相关问题此处

This isn't possible in the current version of F#, unfortunately. See related question here.

这篇关于特定通用类型的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆