How to define optional parameters in F# modules?

家住魔仙堡 提交于 2020-01-16 08:38:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!