Skip to content
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

Implement generalized iteration for looping over dictionaries and arrays with the index/key #2686

Open
Daw588 opened this issue May 3, 2024 · 3 comments

Comments

@Daw588
Copy link

Daw588 commented May 3, 2024

Luau on May 2022 received an update which implemented generalized iteration. Generalized iteration no longer requires developers to write pairs or ipairs. From my knowledge, roblox-ts has only one way to iterate through arrays and dictionaries with the index/key, which is the pairs() function, but it compiles to pairs which is no longer preferred.

Input:

const dictionary = {
	a: 1,
	b: 2,
	c: 3
};

for (const [i, v] of pairs(dictionary)) {
	print(i, v);
}

Output:

local dictionary = {
	a = 1,
	b = 2,
	c = 3,
}
for i, v in pairs(dictionary) do
	print(i, v)
end

I would want a way to make compiler output the code snippet below. However, I don't think it is a simple matter of just changing the output for pairs function, as a developer would expect it to generate the Luau pairs function. Perhaps we could make a macro function that acts like pairs but compiles to generalized iteration.

local dictionary = {
	a = 1,
	b = 2,
	c = 3,
}
for i, v in dictionary do
	print(i, v)
end
@Daw588 Daw588 changed the title Compile pairs() macro into generalized iteration instead of pairs Implement generalized iteration for looping over dictionaries and arrays with the index May 3, 2024
@Daw588 Daw588 changed the title Implement generalized iteration for looping over dictionaries and arrays with the index Implement generalized iteration for looping over dictionaries and arrays with the index/key May 3, 2024
@zwinplayer64
Copy link

This doesn't make sense, pairs should compile to pairs and not generalized iteration

@Daw588
Copy link
Author

Daw588 commented May 12, 2024

I never stated in the post that pairs should compile to generalized iteration. I was asking for a way to iterate through dictionaries and arrays that would produce generalized iteration in the compiler output.

@Dionysusnu
Copy link
Contributor

We've considered this before by giving objects a generic [Symbol.iterator] K,V definition. I'm not sure what the result of the discussion was back then, I can have a look later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants