简体   繁体   English

如何在 F# 模块中定义可选参数?

[英]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.我正在尝试用 F# 重写一个很小的 ​​C# 库,但遇到了一个错误。 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.我已经检查过为什么你不能在松散的函数中使用它们,但是当输入static membermember我得到了另一个错误

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.在阅读了有关它的Microsoft Docs文章后,我认为这会起作用,因为这是我理解您键入它的方式。

By using another way to define the "static class" you can add member to it.通过使用另一种方式定义“静态类”,您可以向其中添加member And then you can use optional parameters.然后你可以使用可选参数。 The class will show up as a normal static class in C#.该类将显示为 C# 中的普通静态类。

[<AbstractClass; Sealed>]
type Kingdom private () =
    static member Rule (?years : int) = ()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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