Giter Club home page Giter Club logo

Comments (7)

mexx avatar mexx commented on June 18, 2024

Looks like the implementation of the TryWith, TryFinally and other build upon those functions are flawed.

If you look at the implementation of TryFinally (code):

member this.TryFinally(body, compensation) =
    try this.ReturnFrom(body())
    finally compensation() 

Lets replace the call to ReturnFrom with it's body, and add a type on the body parameters

member this.TryFinally(body: unit -> AsyncResult<_>, compensation) =
    try body()
    finally compensation() 

Can you spot the problem?

The body function is returning an Async, it might not have completed yet. However the control flow in the TryFinally function already goes on, and as the execution of the body function already ended, the finally block is executed with the compensation function.

If you compare the internals of this Builder with the internals of the AsyncBuilder from the FSharp.Core you can see, that there the try-finally and try-with constructs are lifted into the Async world. In this version of the builder the constructs are left outside, that's why you have the difference in the behaviour.

from domainmodelingmadefunctional.

kerams avatar kerams commented on June 18, 2024

Think it makes sense, thanks.

from domainmodelingmadefunctional.

kerams avatar kerams commented on June 18, 2024

@mexx I changed the 2 methods like this:

member this.TryWith(body, handler) = async {
    try
        let! b = body()
        return this.ReturnFrom(b)
    with e ->
        return handler e }

member this.TryFinally(body, compensation) = async {
    try
        let! b = body()
        return this.ReturnFrom(b)
    finally
        compensation() }

The dispose works as expected now, but I'm not sure if it's really as simple as this, because the AsyncBuilder implementation of these is a lot more involved (perhaps because of doing optimizations?).

from domainmodelingmadefunctional.

mexx avatar mexx commented on June 18, 2024

@kerams I'm not certain that the calls to this.ReturnFrom are even necessary.

It's that simple, the AsyncBuilder implementation can't use any syntax sugar, like you do now with the async.

from domainmodelingmadefunctional.

kerams avatar kerams commented on June 18, 2024

Alright, thanks a lot!

from domainmodelingmadefunctional.

allentiak avatar allentiak commented on June 18, 2024

Just curious... Is this one fixed by #10?

from domainmodelingmadefunctional.

swlaschin avatar swlaschin commented on June 18, 2024

(Finally revisiting all these issues!)
I think this is fixed now. Closing

from domainmodelingmadefunctional.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.