如何在F#模块中定义可选参数? [英] How to define optional parameters in F# modules?

查看:63
本文介绍了如何在F#模块中定义可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用F#重写一个小的C#库,但遇到错误.我正在尝试为模块中的方法定义可选参数,但是编译器会说仅在类型成员上允许使用可选参数".

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".

我已经检查了为什么不能在松散的函数中使用它们,但是在键入static membermember时,我会收到另一个错误. /p>

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

在阅读推荐答案

通过使用另一种方法来定义静态您可以在其中添加member.然后,您可以使用可选参数.

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

这篇关于如何在F#模块中定义可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆