4 people like it.

IDisposable in a Type

This sample illustrates how to use an IDisposable inside an object, as well as a simple use of object expression

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
open System

let getdisposable () = {new IDisposable with 
                           member x.Dispose() = printfn "I am disposed"}

type Atype () =
   let d = getdisposable ()
   interface IDisposable with
      member x.Dispose() = d.Dispose()

                        
let f ()= 
   use l = getdisposable ()
   printfn "hello"

f()//hello -> I am disposed


let f2 ()= 
   use a = new Atype()
   printfn "hello"

f2()//hello -> I am disposed
namespace System
val getdisposable : unit -> IDisposable

Full name: Script.getdisposable
type IDisposable =
  member Dispose : unit -> unit

Full name: System.IDisposable
val x : IDisposable
IDisposable.Dispose() : unit
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Multiple items
type Atype =
  interface IDisposable
  new : unit -> Atype

Full name: Script.Atype

--------------------
new : unit -> Atype
val d : IDisposable
val x : Atype
override Atype.Dispose : unit -> unit

Full name: Script.Atype.Dispose
val f : unit -> unit

Full name: Script.f
val l : IDisposable
val f2 : unit -> unit

Full name: Script.f2
val a : Atype
Raw view Test code New version

More information

Link:http://fssnip.net/dM
Posted:12 years ago
Author:nicolas2
Tags: idisposable , type