问题
I'm trying to re-write a tiny C# lib in F# and I've encountered an error. I'm trying to define optional parameters for a method in a module but the compiler says "Optional arguments are only permitted on type members".
I've checked why you can't use them in loose functions but when typing static member or member I get another error instead.
module Kingdom =
let Rule (?years : int) =
()
I thought this was going to workas it's how I understood you type it, after reading the Microsoft Docs article about it.
回答1:
By using another way to define the "static class" you can add member
to it. And then you can use optional parameters.
[<AbstractClass; Sealed>]
type Kingdom private () =
static member Rule (?years : int) = ()
来源:https://stackoverflow.com/questions/56597045/how-to-define-optional-parameters-in-f-modules