Howto write a function taking variable number of arguments in F#

前端 未结 2 960
半阙折子戏
半阙折子戏 2020-12-20 10:45

I\'ve got a function in C#, and I\'d like to port it (among some other stuff) over to F#, just for the sake of doing it. Unfortunately, I just hit a case for which there see

相关标签:
2条回答
  • 2020-12-20 11:25

    Are you wanting to call this method from F# or C#? This hubFS post seems to indicate that F# doesn't support parameter arrays on the calling side, but I suspect if you use a normal type annotation for the parameter to make it an array, and also decorate it with ParamArrayAttribute, you should be able to call it with variable arguments from C#.

    Can't say I've seen how to decorate a parameter with attributes in F# yet, but I can hunt around if you want... This blog post gives a couple of examples, but not at the parameter level.

    0 讨论(0)
  • 2020-12-20 11:28

    A params array is simply an array with an attribute, as Jon notes. Add the attribute before the parameter.

    let test ([<ParamArray>] arr : 'a array) = 
        if arr.Length = 0 then invalid_arg "arr"
        // ....
    

    You don't need to specify the type:

    let test ([<ParamArray>] arr) = ... // lets type inference do its thing
    

    But... pattern matching doesn't work on the array type. You could write an active pattern to help. Basically, you have to decide what's more important: the F# code or the C# code. The same tradeoff will apply as you design higher order functions, use tuples, use discriminated unions, etc. C# can't express most things, and F# doesn't currently support some of the little bits of sugar C# has (Expression Tree writing in the compiler, for example).

    0 讨论(0)
提交回复
热议问题