例如,如果我用 F# 编写了一个模块
module Lib
type A =
member this.x1 x = ...
let helpa x = ...
let helpb x = ...
type B =
member this.y1 x = ...
let helpc x = ...
typeA with
member this.x2 x = ...
typeB with
member this.y2 x = ...
它在 F# 中运行良好open Lib
,但是,如果我想在 C# 中使用它(我只对类型和成员函数感兴趣)Lib
),每次我创建一个类型时,我都必须new Lib.A(...)
。无法省略模块名称变得相当烦人。调用静态方法,例如Lib.A.C()
更是麻烦。
然后我尝试更换module
with namespace
,每次引入一些辅助函数时,我都必须使用新名称创建一个新模块。有时,我可以设法将所有辅助函数重新排列到 1 个模块中,但这会导致代码可读性较差。
对此,更好的结构是什么?
希望我有:Using * = Lib.*
for C#.
F# 在这里比 C# 提供了更多的灵活性,因此我将以标准方式将其公开给 C#,即将类型包含在命名空间中。我认为这样的东西提供了两全其美的优点:
namespace Lib
type A =
member this.x1 x = ()
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module A =
let helpa x = ()
let helpb x = ()
type B =
member this.y1 x = ()
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module B =
let helpb x = ()
type A with
member this.x2 x = ()
type B with
member this.y2 x = ()
F# 集合遵循类似的设计。您可以使用[<AutoOpen>]
and [<RequireQualifiedAccess>]
属性以进一步控制如何从 F# 使用模块。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)