-
Notifications
You must be signed in to change notification settings - Fork 471
[PoC] for..of loops #7887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
[PoC] for..of loops #7887
Conversation
compiler/ml/ast_mapper_to0.ml
Outdated
(sub.expr sub e3) | ||
| Pexp_for_of (_pat, _array_expr, _body_expr) -> | ||
(* Convert for...of to an extension since it doesn't exist in old AST *) | ||
extension ~loc ~attrs (Location.mkloc "rescript.for_of" loc, PStr []) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what needs to be done here.
How can we map to ast0 in such a way that we can map it back?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extension sounds reasonable.
2 │ | ||
|
||
Did you forget a `in` here? | ||
I'm not sure what to parse here when looking at "eof". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what to do about this change in the test output here
5 │ Js.log("for") | ||
6 │ } | ||
|
||
consecutive statements on a line must be separated by ';' or a newline |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or here
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
c508d37
to
77f91d8
Compare
The big questions were language design related ones, rather than implementation. |
Frankly, importing the JavaScript for-of syntax as-is doesn't sound all that appealing. It's not much different from embedding Here are my thoughts on designing for loops:
|
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. |
Early return keeps on coming up as a topic. |
For me, the for..of feature as implemented here would already be useful in and of itself because it
let asyncProcess = async () => {
let results = []
for item of arr {
let result = await processData(item)
results->Array.push(result)
}
results
}
for item for arr {
# vs.
arr->Array.forEach(item =>
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. |
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. |
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.