syntax - F# 构造函数语法 - 覆盖和扩充 new

标签 syntax f# constructor

我有一个具有打开/关闭语法的非一次性类,我希望能够 use ,所以我试图继承它,并将 Open 工作到 new和 Close into Dispose。

第二部分还可以,但我不知道如何进行 Open:

type DisposableOpenCloseClass(openargs) =
    inherit OpenCloseClass()
    //do this.Open(openargs)  <-- compiler no like
    interface IDisposable
        with member this.Dispose() = this.Close()

(参见 this question 我很久以前问过的,但我不能把点加到这个上)

最佳答案

key 是 as this :

type OpenCloseClass() =
    member this.Open(x) = printfn "opened %d" x
    member this.Close() = printfn "closed"

open System

type DisposableOpenCloseClass(openargs) as this = 
    inherit OpenCloseClass() 
    do this.Open(openargs)
    interface IDisposable 
        with member this.Dispose() = this.Close() 

let f() =
    use docc = new DisposableOpenCloseClass(42)
    printfn "inside"

f()

关于syntax - F# 构造函数语法 - 覆盖和扩充 new,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3012038/

相关文章:

.net - 如何在 F# 中传递 .net 对象值?

types - 公共(public)记录类型的构造函数?

c# - 是否有使用继承构造函数的约定

bash - 如何迭代 Bash 中变量定义的一系列数字?

javascript - 这个 JavaScript 语法/模式是什么意思?

c++ - 从 C 入门学习。预期的表达式错误 Xcode

python - 如何在python中不指定默认参数?

f# - 如何设置 Visual Studio 代码 launch.json 文件以调试 F#?

c++ - 模板忽略了常量(为什么?)

c++ - 如何在C++中初始化成员的构造函数参数