How can unify the signature of this member method and the inline function

点点圈 提交于 2019-12-24 00:25:24

问题


given this code

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)

I get this error

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

I assume that signature of my static member can not really be unified to (^U -> ^T) -> ^T -> ^T

How can I solve this?


回答1:


Looking at the previous question (i.e. switching back to the member function) and your comments, perhaps this would work:

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


来源:https://stackoverflow.com/questions/37855555/how-can-unify-the-signature-of-this-member-method-and-the-inline-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!