gpt4 book ai didi

f# - 为什么这个内联元组不起作用?

转载 作者:行者123 更新时间:2023-12-04 18:01:33 26 4
gpt4 key购买 nike

为什么在这种情况下内联不起作用?

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com