如何实现具有可变数量参数的方法?
问题描述:
如何实现具有可变数量参数的方法?如何实现具有可变数量参数的方法?
In C#, we can use the params keyword:
public class MyClass
{
public static void UseParams(params int[] list)
{
for (int i = 0; i < list.Length; i++)
{
Console.Write(list[i] + " ");
}
Console.WriteLine();
}
}
所以,我该怎么办这在F#?
type MyClass() =
member this.SomeMethod(params (args:string array)) =()
我收到以下错误从上面的代码:
The pattern discriminator 'params' is not defined
答
您可以使用ParamArrayAttribute
:
type MyClass() =
member this.SomeMethod([<ParamArray>] (args:string array)) = Array.iter (printfn "%s") args
则:
let mc = MyClass()
mc.SomeMethod("a", "b", "c")
+0
如何一次传递数组? – Mohsen
[在F#中的可能的复制,你如何咖喱ParamArray函数(li ke sprintf)?](http://stackoverflow.com/questions/11145680/in-f-how-do-you-curry-paramarray-functions-like-sprintf) –