2

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 wor kas it's how I understood you type it, after reading the Microsoft Docs article about it.

Mikael Dúi Bolinder
  • 2,080
  • 2
  • 19
  • 44
  • 2
    The docs link you provided states 'optional parameters are permitted only on members, not on functions created by using let bindings'. I don't believe you can have an optional parameter on a top level function in F#. – Joe Clay Jun 14 '19 at 12:01

1 Answers1

2

By using another way to define the "static class" you can add member to it. And then you can use optional parameters. The class will show up as a normal static class in C#.

[<AbstractClass; Sealed>]
type Kingdom private () =
    static member Rule (?years : int) = ()
Mikael Dúi Bolinder
  • 2,080
  • 2
  • 19
  • 44