gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:21:49 26 4
gpt4 key购买 nike

我有一个具有打开/关闭语法的非一次性类,我希望能够 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com