本文介绍了如何才能统一此成员方法和内联函数的签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出此代码

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

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

我该如何解决?

推荐答案

查看(即切换回成员函数)和您的评论,也许可以这样做:

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

这篇关于如何才能统一此成员方法和内联函数的签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 21:11