如何统一此成员方法和内联函数的签名

时间:2016-06-16 09:44:36

标签: f#

给出此代码

type Baz = Baz of int with
    static member bar f (Baz(b)) = f b

let inline foo< ^T, ^U when ^T : (static member bar : (^U -> ^T) -> ^T -> ^T)>
    (f:(^U -> ^T)) (t:^T) : ^T = 
    (^T : (static member bar : (^U -> ^T) -> ^T -> ^T) f, t )

let x = foo (fun x -> (Baz 0)) (Baz 1)

我收到此错误

error FS0043: Method or object constructor 'bar' not found

我认为我的静态成员的签名无法真正统一到(^U -> ^T) -> ^T -> ^T

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

查看previous question(即切换回成员函数)和您的评论,也许这会有效:

type Baz = Baz of int with
    member this.bar (f: 'a -> 'b): 'b = match this with
                                        | Baz i ->  f i

let inline foo (f: ^U -> ^T) (t:^T)  = 
    let foo' = (^T : (member bar : (^U -> ^T) -> ^T) (t, f))
    foo'

let x = foo (fun x -> (Baz 0)) (Baz 1)

// This returns Baz 0
printfn "%A" x