rescript-compiler icon indicating copy to clipboard operation
rescript-compiler copied to clipboard

[PoC] for..of loops

Open cknitt opened this issue 3 months ago • 7 comments

My assistant and I created a very basic implementation of for...of loops that only works on arrays for now.

Will need to think about how to support iterators later.

cknitt avatar Sep 13 '25 06:09 cknitt

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7887
@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7887
@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7887
@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7887
@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7887
@rescript/runtime

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/runtime@7887
@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7887

commit: 8b779af

pkg-pr-new[bot] avatar Sep 13 '25 07:09 pkg-pr-new[bot]

The big questions were language design related ones, rather than implementation. There are many ways to go about this. @cometkim still got some design notes from the retreat? This could be the right time to do some more text-based discussion before the code-based one.

cristianoc avatar Sep 18 '25 23:09 cristianoc

Frankly, importing the JavaScript for-of syntax as-is doesn't sound all that appealing. It's not much different from embedding %raw.

Here are my thoughts on designing for loops:

  1. Since iterators are essential in JavaScript, we need to define iterator semantics before design the synyax. The syntax follows.
  2. Without control flow support, the for-of syntax itself isn't very useful. With good control semantics, the simple iterator protocol and even the MoonBit style loop syntax become useful.
  3. The current for x into y syntax is valid for index-based access, but the JS' for-of doesn't. To combine the benefits of both style, generator (for sequencing) support will be needed. Zig's for loop design is a good example.
  4. Are for statements enough? To maintain a robust type-based approach, it would be better to design them based on expressions.

cometkim avatar Sep 19 '25 00:09 cometkim

@cometkim still got some design notes from the retreat?

https://github.com/JonoPrest/iterator-rescript/blob/retreat-note/RETREAT_NOTE.md

On the last retreat, we only discussed iterator semantics and simulating break/continue; we also need to deal with the early-return or jump (for escaping nested loops) semantics.

cometkim avatar Sep 19 '25 00:09 cometkim

Early return keeps on coming up as a topic. Perhaps it makes sense to explore that as a prerequisite to this.

cristianoc avatar Sep 19 '25 02:09 cristianoc

For me, the for..of feature as implemented here would already be useful in and of itself because it

  1. allows the usage of await like in this example from the tests which would not be possible using arr->Array.forEach(...)
let asyncProcess = async () => {
  let results = []
  for item of arr {
    let result = await processData(item)
    results->Array.push(result)
  }
  results
}
  1. is more concise
for item for arr {
# vs.
arr->Array.forEach(item =>
  1. is more performant

  2. Increases our coverage of JS features that JS developer may expect to have

I agree that early return would be nice to have, but not sure if we should tie this to for..of.

I also agree that it would be a good idea to look into how iterators should work. OTOH having this for arrays would be a low-hanging fruit that could hopefully be extended to iterators later.

cknitt avatar Sep 19 '25 06:09 cknitt

OTOH having this for arrays would be a low-hanging fruit that could hopefully be extended to iterators later.

This is already a dangerous assumption. for-of loops are all about iterators, not arrays.

It should be designed to interact with iterator types. When it works with array types, it leads users to copy arbitrary iterators into arrays unnecessarily.

cometkim avatar Sep 22 '25 07:09 cometkim