Generics 特定泛型类型的扩展方法
我试图为绑定到F#中特定泛型类型参数的泛型类型创建各种扩展方法,但该语言似乎不允许我: 我想做的事情如下:Generics 特定泛型类型的扩展方法,generics,f#,extension-methods,type-constraints,generic-type-argument,Generics,F#,Extension Methods,Type Constraints,Generic Type Argument,我试图为绑定到F#中特定泛型类型参数的泛型类型创建各种扩展方法,但该语言似乎不允许我: 我想做的事情如下: type IEnumerable<int> with member this.foo = this.ToString() 有没有办法在F#中实现这一目标?也许我只是使用了错误的语法?如果没有,我希望有人能建议一种解决方法,也许在某个地方使用类型约束。好的,您可以使用约束,但不能使用像int这样的密封类型 type IEnumerable<'a w
type IEnumerable<int> with
member this.foo =
this.ToString()
有没有办法在F#中实现这一目标?也许我只是使用了错误的语法?如果没有,我希望有人能建议一种解决方法,也许在某个地方使用类型约束。好的,您可以使用约束,但不能使用像int这样的密封类型
type IEnumerable<'a when 'a :> InheritableType> =
member this.Blah =
this.ToString()
type IEnumerable InheritableType>=
请记住,诸如此类=
this.ToString()
嗯…不幸的是,这在当前版本的F#中是不可能的。参见相关问题。通用扩展方法现在在F#3.1中提供:
open System.Runtime.CompilerServices
open System.Collections.Generic
[]
类型Utils()=
[]
静态成员内联Abc(obj:IEnumerable)=obj.ToString()
printfn“%A”([1..10].Abc())
为了帮助其他人寻找类似的解决方案,下面是一个示例,演示如何使用带有类型约束的泛型扩展方法。在下面的示例中,有一个类型约束要求传递的类型参数公开默认构造函数。这是使用应用于订单
记录的[]
属性完成的。此外,我还将方法的结果约束为传递的类型
要使用扩展方法,必须指定要使用的类型。注意,我还扩展了一个通用字典接口
[<Extension>]
type ExtensionMethds () =
[<Extension>]
static member inline toObject<'T when 'T: (new: unit -> 'T)> (dic: IDictionary<string,obj>): 'T =
let instance = new 'T()
// todo: set properties via reflection using the dictionary passed in
instance
[<CLIMutable>]
type Order = {id: int}
let usage =
let dictionaryWithDataFromDb = dict ["id","1" :> obj]
let theOrder = dictionaryWithDataFromDb.toObject<Order>()
theOrder
[]
类型ExtensionMethds()=
[]
静态成员内联toObject'T)>(dic:IDictionary):'T=
让实例=new'T()
//todo:使用传入的字典通过反射设置属性
实例
[]
类型顺序={id:int}
让用法=
让dictionaryWithDataFromDb=dict[“id”,“1”:>obj]
让theOrder=dictionaryWithDataFromDb.toObject()
理论家
谢谢你的建议,但事实上,我使用的是密封类型,所以它不太有效。这可能与你认为的不太一样。。。实际上,您正在定义一个新的IEnumerable
类型-尝试在现有的IEnumerable
上调用扩展。内部F#bug数据库“4548:支持特定类型实例化的扩展方法”中的一条建议跟踪了此功能,但它不太可能成为VS2010版本的切入点。
open System.Runtime.CompilerServices
open System.Collections.Generic
[<Extension>]
type Utils () =
[<Extension>]
static member inline Abc(obj: IEnumerable<int>) = obj.ToString()
printfn "%A" ([1..10].Abc())
[<Extension>]
type ExtensionMethds () =
[<Extension>]
static member inline toObject<'T when 'T: (new: unit -> 'T)> (dic: IDictionary<string,obj>): 'T =
let instance = new 'T()
// todo: set properties via reflection using the dictionary passed in
instance
[<CLIMutable>]
type Order = {id: int}
let usage =
let dictionaryWithDataFromDb = dict ["id","1" :> obj]
let theOrder = dictionaryWithDataFromDb.toObject<Order>()
theOrder