Resource management when using "use" inside a sequence expression

I have a simple sequence expression that uses a resource that I would like to clear when done:

type MyObject() =
    member this.Items =
        seq {
            use resource = ResourcePool.Get()
            let c = resource.ValueCount
            if c > 0 then
                for i in 0 .. c - 1 do
                    yield resource.GetValue i
        }

If I then use the sequence to iterate, say, halfway through the elements, when will the resource be Disposed?

For example:

// ...

let foo = MyObject
let item = foo.Items |> Seq.find ( fun i -> i.Name = "name" )

// ...

Will resourceSeq.find be deleted after completion? Or do I need to rethink my resource management strategy?

+4
source share
1 answer

, IEnumerator , , . Seq , ( . tryFind ). GetEnumerator , , use let.

+5

Source: https://habr.com/ru/post/1523597/


All Articles