f#中的可变长度元组 [英] Variable length tuples in f#
问题描述
是否可以编写一个函数来接受可变长度的元组?我正在尝试编写一种可以这样调用的方法:
Is it possible to write a function to accept a tuple of variable length? I'm trying to write a method that can be called like this:
let a = sum(1,2)
let b = sum(1,2,3)
可以将其解释为带参数的函数调用吗?还是该方法需要用c#编写:
Could it be interpreted as a function call with params? Or would the method need to be written in c#:
double sum(params object[] double) {
...
}
推荐答案
否-元组在定义上不是可变长度,并且要编写这样的函数,您需要在C ++中进行模板元编程之类的东西,并且没有F#中有这样的事情; let inline
也不帮助您.
No - tuples are by definition not variable length, and to write a function like this you'd need something like template metaprogramming in C++ - and there isn't such a thing in F#; let inline
won't help you there either.
当然,如果您选择一个列表,它看起来不会有太大不同:
Of course, if you take a list instead, it won't look that much different:
sum[1; 2]
sum[1; 2; 3]
这篇关于f#中的可变长度元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!