从C#调用F#函数而没有丑陋的"模块"后缀

Mil*_*oDC 2 c# f# interop module

我更喜欢为F#以惯用方式将函数与类型分开:

[<Struct>]
type Vec2 =
    {
        x   : single
        y   : single
    }

    static member inline (/) (v, scalar) =
        let s = single scalar in { x = v.x / s; y = v.y / s }

[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module Vec2 =
    let inline length v = ((pown v.x 2) + (pown v.y 2)) |> sqrt

    let unit v = let l = length v in { x = v.x / l; y = v.y / l }
Run Code Online (Sandbox Code Playgroud)

不幸的是,在C#中,lengthunit函数是Vec2Module.lengthVec2Module.unit. 呸.

除了将函数定义为静态成员之外Vec2,还有什么解决方法?

更新:

感谢您的回复,这些解决方案可以解决因使用Module后缀而受到限制的问题.

我不想为每个以这种方式编写的模块做出明确的声明,所以我只是坚持使用静态成员方法而不是模块中的绑定let定义.

顺便说一句,我想知道是否已将此问题考虑在未来的C#版本中.例如,在使用属性装饰类型,方法,属性等时,不要使用Attribute后缀.Module在某些时候,后缀可能会发生类似的事情吗?

小智 5

在C#6.0中,您可以访问静态成员而无需指定类型名称.在你的情况下:

using static Vec2Module;
Run Code Online (Sandbox Code Playgroud)


Fyo*_*kin 5

您可以使用using指令为类创建本地别名:

using Vec2 = Vec2Module;
Run Code Online (Sandbox Code Playgroud)