为什么在这种情况下内联不起作用?
type TupleBuilder () =
static member inline Cons(a,(b,c)) = (a, b, c)
static member inline Cons(a,(b,c,d)) = (a, b, c, d)
static member inline Cons(a,(b,c,d,e)) = (a, b, c, d, e)
let inline cons h t = TupleBuilder.Cons(h,t)
调用
TupleBuilder.Cons
给了我以下编译器错误A unique overload for method 'Cons' could not be determined based on type
information prior to this program point. A type annotation may be needed.
Candidates:
static member TupleBuilder.Cons : a:'a0 * ('a1 * 'a2 * 'a3 * 'a4) -> 'a0 * 'a1 * 'a2 * 'a3 * 'a4,
static member TupleBuilder.Cons : a:'a0 * ('a1 * 'a2 * 'a3) -> 'a0 * 'a1 * 'a2 * 'a3,
static member TupleBuilder.Cons : a:'a0 * ('a1 * 'a2) -> 'a0 * 'a1 * 'a2
最佳答案
单独的内联不会延迟调用站点的过载决定。
您需要添加一个 type A or type B
在过载调用中。
在这种情况下,您可以使用二元运算符轻松完成:
type TupleBuilder () =
static member inline ($) (_:TupleBuilder, (b,c)) = fun a -> (a, b, c)
static member inline ($) (_:TupleBuilder, (b,c,d)) = fun a -> (a, b, c, d)
static member inline ($) (_:TupleBuilder, (b,c,d,e)) = fun a -> (a, b, c, d, e)
let inline cons h t = (TupleBuilder() $ t) h
// val inline cons : h:'a -> t: ^b -> 'c
when (TupleBuilder or ^b) : (static member ( $ ) : TupleBuilder * ^b -> 'a -> 'c)
更多关于元组的内联乐趣,请查看 this old blog post .
关于f# - 为什么这个内联元组不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46568998/