Do blocks #146
Replies: 2 comments 7 replies
-
Thanks for the great suggestion. I think this effectively boils down to having full support for We can start by allowing them in simple cases (like the right hand side of an assignment) and expand to covering more and more cases. A possible transpilation for your example const fetchMenu = async () => {
let restaurant
{
const response = await fetch(...)
if (!response.ok) return null
const body = await response.json()
restaurant = body.restaurant
}
let products1
{
const response = await fetch(...)
if (!response.ok) return null
const body = await response.json()
products1 = body.products
}
const products = products1
return { ...restaurant, products }
} |
Beta Was this translation helpful? Give feedback.
-
Just a note that I'd like to also support the following syntax (with fetchMenu := async () =>
restaurant .= do
response := await fetch(...)
if !response.ok return null
body := await response.json()
body.restaurant
products := do
response:= await fetch(...)
if !response.ok return null
body := await response.json()
body.products
{ ...restaurant, products } As such I'd expect it to be more or less compatible with #58 and the |
Beta Was this translation helpful? Give feedback.
-
👉 I think you're already working on something like this, but I'm still sending the proposal because it specifies a very important behavior with early-returns.
Sometimes you want to calculate a value with some intermediate variables and you want to limit the exposure of those variables as much as possible to ensure they're not used anywhere else. Look at ReScript's blocks for an example of such syntax: https://rescript-lang.org/docs/manual/latest/overview#blocks-1.
So I propose support for this:
Besides limiting the exposure of intermediate variables, it also makes the code more readable. At each indent level you have a different level of detail, so you can quickly see that in the second indent level you're doing basically 3 things:
If you want more details, then you read what's inside each block. This is important because we spend more time reading code than writing, and this makes it easier to read.
Of course, it's also possible to extract those blocks into separate functions, but then you have to scroll up and down to change the level of detail you're reading. Reading from top to bottom, in the exact order it actually happens, is easier IMHO. It's like being able to zoom in and zoom out.
Note that transpiling the blocks to an IIFE wouldn't be enough because the idea is that any
return
inside the blocks make thefetchMenu
function early-return, and areturn
inside an IIFE would actually returnnull
to the variable but keep executing the function.Edits
do
and indentation.Beta Was this translation helpful? Give feedback.
All reactions