Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# constructor syntax - overiding and augmenting new

I have a non-disposable class with Open/Close syntax that I'd like to be able to use, so I'm trying to inherit from it, and work the Open into the new and the Close into Dispose.

The second part is ok, but I can't work out how to do the Open:

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

(cf. this question which I asked a long time ago, but I can't join the dots to this one)

like image 810
Benjol Avatar asked Jun 10 '10 06:06

Benjol


1 Answers

Key is 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()
like image 146
Brian Avatar answered Nov 07 '22 13:11

Brian