内联时扩展方法出错

时间:2016-06-28 09:01:52

标签: f#

我想扩展一些系统类型,然后通过内联使用它们

type System.String with  
    member this.foo n = this + "!" + n 

type System.Boolean with  
    member this.foo n = sprintf "%A!%A" this n 

现在我调用这些扩展方法

let x = "foo".foo "bar"
let y = true.foo "bar"

给了我这个

- val x : System.String = "foobar"
- val y : string = "true!"bar""

所有罚款和花花公子 - 但现在我想将.foo的调用包装成内联函数

let inline foo n v = (^T : (member foo : ^N  -> ^S) v, n)
let z = foo "bar" "baz" 

现在我收到编译错误告诉我

> The type 'string' does not support the operator 'foo':

嗯......确实如此!

有人可以解释发生什么事吗?

2 个答案:

答案 0 :(得分:9)

静态成员约束(this的可能重复)中不考虑扩展方法,当您希望使用成员约束实现通用代码并使其也可以使用已定义或原语时,这是一个普遍问题类型。

请参阅user voice请求,以及[{3}}和here提及的解决方法。

如果您按照那里的链接,您将看到当前的解决方法,它基本上涉及为所有已知类型创建一个中间类型和重载,并为扩展创建一个通用类型。

这是如何解决它的一个非常基本的例子:

type Foo = Foo with
    static member ($) (Foo, this:int)    = fun (n:int) -> this + n 
    static member ($) (Foo, this:string) = fun n -> this + "!" + n 
    static member ($) (Foo, this:bool)   = fun n -> sprintf "%A!%A" this n 

let inline foo this n = (Foo $ this) n

//Now you can create your own types with its implementation of ($) Foo.

type MyType() =
    static member ($) (Foo, this) = 
        fun n -> printfn "You called foo on MyType with n = %A" n; MyType()

let x = foo "hello" "world"
let y = foo true "world"
let z = foo (MyType()) "world"

您可以通过为新类型添加显式泛型重载来增强它:

// define the extensions

type System.String with  
    member this.foo n = this + "!" + n 

type System.Boolean with  
    member this.foo n = sprintf "%A!%A" this n 

// Once finished with the extensions put them in a class
// where the first overload should be the generic version.
type Foo = Foo with
    static member inline ($) (Foo, this) = fun n -> (^T : (member foo : ^N -> ^S) this, n)
    static member ($) (Foo, this:string) = fun n -> this.foo n 
    static member ($) (Foo, this:bool)   = fun n -> this.foo n
    // Add other overloads
    static member ($) (Foo, this:int)    = fun n -> this + n 

let inline foo this n = (Foo $ this) n

//later you can define any type with foo
type MyType() =
    member this.foo n = printfn "You called foo on MyType with n = %A" n; MyType()

// and everything will work
let x = foo "hello" "world"
let y = foo true "world"
let z = foo (MyType()) "world"

您可以通过手动编写静态约束并使用成员而不是运算符来进一步优化它(请参阅示例Don Syme's explanation of why it's complicated to implement it in the F# compiler),

在一天结束时,你最终会得到类似FsControl的here函数。

答案 1 :(得分:2)

静态解析的类型约束不支持扩展方法。它不是F#的特色。

如果您希望F#获得对更高级别多态性的支持,您可以vote for it on user voice