Git Product home page Git Product logo

Comments (10)

jwosty avatar jwosty commented on May 13, 2024 4

@rmunn Hmm, good point, that's an asymmetry. About the chaining concern first, IIRC, this next code block would get compiled like so, no?

// sugared
builder {
    let! x = xexpr
    return f x
    let! y = yexpr
    return g y }
// desugared
builder.Bind(xexpr, fun x ->
    builder.Combine
       (builder.Return(f x),
        builder.Bind(fun y ->
            builder.Return(g y)
        )
    )
)

First of all, I don't know the semantics of return vs yield should be a concern of this feature.

Putting aside the normal semantics of return, it we wanted to allow this to use map, I don't see any other way than:

builder.Combine(builder.Map(fun x -> f x),
                builder.Map(fun y -> g y))

Also, this would not be possible if x is used anywhere else in the expression. If x is being used later somewhere else, even in the next expression, this transformation would no longer be possible (which I think makes sense; it's not following the semantics of map anymore).

As for the other issue, what if we introduce a MapYield method as well? That way, it would be completely optional, and could have different semantics than Map. Come to think of it, maybe we should call "Map" "MapReturn" instead.

from fslang-design.

davidglassborow avatar davidglassborow commented on May 13, 2024 1

To quote https://fsharpforfunandprofit.com/posts/computation-expressions-builder-part1/#introducing-yield

At this point you might be wondering: if return and yield are basically the same thing, why are there two different keywords? The answer is mainly so that you can enforce appropriate syntax by implementing one but not the other. For example, the seq expression does allow yield but doesn’t allow return, while the async does allow return, but does not allow yield, as you can see from the snippets below.
In fact, you could create slightly different behavior for return vs. yield, so that, for example, using return stops the rest of the computation expression from being evaluated, while yield doesn’t.

More generally, of course, yield should be used for sequence/enumeration semantics, while return is normally used once per expression. (We’ll see how yield can be used multiple times in the next post.)

from fslang-design.

TD5 avatar TD5 commented on May 13, 2024 1

It's probably be worth thinking about how this interacts with Apply.

I suspect we want let! ... return ... to be desugared by MapReturn if it is defined, else Apply & Return if they are defined, else Bind and Return.

from fslang-design.

rmunn avatar rmunn commented on May 13, 2024

Something to consider is whether this should also apply to let! followed by yield, as well as let! followed by return. Since both yield and return have type 'T -> M<'T>, they are essentially the same operation from a monadic point of view, and let! followed by yield could also be compiled to map. But do yield and return have the same semantics from the point of view of the programmer using a CE builder? E.g., would this be meaningful?

seq {
    let! x = someSeqProducingFun()
    yield g x
    let! y = anotherSeqProducingFun()
    yield h y
}

One major difference between yield and return, of course, is the intended usage in CEs: return is intended to be used once, for a CE that conceptually returns "a single value" (for whatever "value" and "single" mean in that given CE), such as an async. While yield is intended to be called multiple times, in CEs that conceptually return multiple values, like seq. So the ultimate question is, is it meaningful to have multiple let! -> yield operations (as in the above) translated to map in a CE such as seq?

from fslang-design.

gusty avatar gusty commented on May 13, 2024

Apart from style conventions regarding return and yield, I think there is technically a difference wrt how Custom Operations maintain variable space.

from fslang-design.

davidglassborow avatar davidglassborow commented on May 13, 2024

cc @dsyme

from fslang-design.

jwosty avatar jwosty commented on May 13, 2024

Any more thoughts on this?

from fslang-design.

devvydeebug avatar devvydeebug commented on May 13, 2024

There's no mention of do! ... return ... in the current RFC. I suppose it'd be supported, wouldn't it?

from fslang-design.

cartermp avatar cartermp commented on May 13, 2024

@mistiara That would have to be considered, yes.

from fslang-design.

jwosty avatar jwosty commented on May 13, 2024

I just read over FS-1063 (RFC). If I am not mistaken, it now covers the cases that this RFC (FS-1048) address. Closing as this RFC is now redundant.

from fslang-design.

Related Issues (20)

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.