4 ms·
I have been informed that computation expressions do not have early return. For example, `async` does allow you to express what appears to be an early return, b
by Smaug123 3y ago
I have been informed that computation expressions do not have early return. For example, `async` does allow you to express what appears to be an early return, but only of the `unit` type, and in fact it doesn't return early; it simply proceeds straight past the `return` keyword. Raised https://github.com/dotnet/fsharp/issues/15759 https://github.com/dotnet/fsharp/issues/15759 .
- eyegor 3y agoYou can create true "early return" behavior by writing a lazy computation builder [0]. It feels esoteric but taking the time to craft a domain specific computation builder can drastically simplify the rest of your code base. I don't know if there's a more modern (more ergonomic) way to do this since this has been around since ~2012 or so. Alternatively you can take the more functional approach and rewrite your function logic to use `if x then (value) else` throughout instead of `return (value)` or `yield (value)`. Early return isn't really a thing in f# but if-else is. [0] https://fsharpforfunandprofit.com/posts/computation-expressions-builder-part5/ https://fsharpforfunandprofit.com/posts/computation-expressi...
- pharmakom 3y agomost of the time early return can be replaced by function calls. what bites is that f# has no way to force TCO so sometimes it's safer to write a loop with a mutable flag.