简体   繁体   中英

How to define optional parameters in F# modules?

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.

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) = ()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM