F#在实现接口时从一个类型的另一个函数调用成员函数

Ale*_* 75 1 f#

这是这里的工作示例:

type MethodExample() = 

    // standalone method
    member this.AddOne x = 
        x + 1

    // calls another method
    member this.AddTwo x = 
        this.AddOne x |> this.AddOne
Run Code Online (Sandbox Code Playgroud)

这就是我想要做的:

type MethodExample() = 

    // standalone method
    member this.AddOne x = 
        x + 1

    // calls another method
    member this.AddTwo x = 
        this.AddOne x |> this.AddOne
Run Code Online (Sandbox Code Playgroud)

AddOne功能不可用,我也尝试将其向下转换为 MethodExample,但它很糟糕并且不起作用。

我该怎么做?

Fyo*_*kin 5

在 F# 中,所有接口实现都是私有的——这意味着接口方法不像在 C# 中那样也作为类的公共方法出现。它们只能通过界面访问。

因此,为了访问它们,您需要先将您的类转换为接口:

member this.AddTwo x = 
    let me = this :> IMethod
    me.AddOne x |> me.AddOne
Run Code Online (Sandbox Code Playgroud)