问题
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