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

标签 f#

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

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/

相关文章:

c# - 从 C# 调用 F# 代码

f# - 什么是错误 "A type instantiation involves a byref type."以及 F# 中的解决方法是什么

f# - 类型初始值设定项上的 NullReferenceException

module - 我可以获得所有程序模块中不是函数的值的列表吗?

c# - 将天数转换为人类可读的持续时间文本

f# - F# 中的 Nullable< >'s and "null"

.net - F# 将内部模块视为私有(private)模块

c# - 如何在具有多个客户端的 TCP 服务器端保留消息发送顺序

f# - 还有其他惯用的方式来传递状态信息吗?

algorithm - 重构算法作为计算表达式?