Conversation
|
Marked it as a draft because it will probably require several changes before it's ready. |
|
Just an early comment for consideration as you flesh it out. What is the reasoning for why comprehension should not be combined with standard lua fields? It is possible that it could follow the same logic as if the comprehension produces duplicate keys. Would be nice to see an extended alternative to "helper functions" of including them in the standard lib. For example having |
|
Fastcall functions can not call user code, yield, fail, or reallocate stack, so if table.map was a library function it would not get special treatment on the C side. |
|
Does this RFC address multiple loops and conditions within a single comprehension? If not, something to consider is what order the With Luau in its current state I personally would prefer comprehensions over Something else to consider is other types of Something else to also consider is allowing mixing >>> a = [[1, 2, 3], None, [4, 5, 6]]
>>> [
... i
... for j in a
... if j
... for i in j
... ]
[1, 2, 3, 4, 5, 6]These suggestions all depend on how "complex" you want to allow comprehensions to be. Python's can be very complex, and therefore very powerful, but the trade-off comes from potential readability. Formatting them properly aids a lot with this, but that complexity may still be undesirable. As is, too, there's nothing in the proposal that would prevent future expansion to any of the above, so it may be preferable to have first a minimal implementation, followed by more complex extensions later. Edit: Just as a reference, given I've mentioned Python a lot in this comment, https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-comprehension and https://docs.python.org/3/reference/expressions.html#grammar-token-python-grammar-dict_comprehension are the explained grammar for how Python handles comprehensions. |
I added standard library helpers as an alternative in the RFC. |
This RFC currently only targets a minimal form of table comprehension. At the moment, the proposal only supports a single Do you think starting with this minimal subset makes sense? |
I'm honestly unsure! As-is, it doesn't restrict their addition in the future, which is really good. I really couldn't say if it makes more sense to have one uber-fleshed-out RFC or one that's a starting point and a second that expands it. The benefit of just starting with a simple RFC is it's potentially more likely to get merged, but the risk of hoping to get a second merged too is if that second doesn't you can end up with a half-baked feature. |
That makes sense. I think it’s safer to establish a simple baseline first and then explore more complex extensions separately if there’s interest. For now, I’m planning to keep the RFC minimal to keep the feature easy to understand and reason about. That said, I’m open to expanding the scope if there’s strong feedback that the minimal version would be too limited. |
|
I always found the -- simple
local luau_files: { string } = {
for _, filename in fs.listdir("./some/path") do
if string.match(filename, "%.luau$") then
filename
else -- like with regular if expressions outside table comprehensions, every if expression must have an else clause
continue
}This makes nested comprehensions much more readable (even if they're more verbose) -- nested
local results = {} :: { [string]: { string } }
local elements_with_meow = {
for name, list in results do
for _, element in list do
if string.match(element, "meow") then
element
else
continue
}I actually wrote a whole table comprehensions RFC on this a few years ago (that also clearly describes the semantics of As a nit, I recommend removing the uses of |
|
I think that kind of design could be worth exploring, but it would likely be better suited as a separate RFC rather than being combined into this one. For this RFC, I’m intentionally keeping the design minimal, while leaving room for more complex forms like nested comprehensions to be explored as future extensions if there’s interest. I'll also remove the use of |
Removing the use of ipairs in examples
|
What does it do other than bloating syntax tree and ruining the intuitive design principle. It doesn't take 2 lines to do the same thing without additional syntax |
While this RFC doesn't introduce new capabilities, the goal of it is to provide a more concise way to represent common table transformation patterns. These transformations can be written within a few lines today, but usually involve boilerplate that can obscure the intent of the code (e.g. table initialization, mutation, and conditional insertion). Using less than 2 lines for said common transformations will make the code unnecessarily more difficult to read, and this proposal focuses on making those patterns easier to express inline rather than expanding what the language can do. |
Rendered
This RFC proposes the feature of table comprehension, similar to languages like Python & MoonScript. This is my first RFC so it's probably pretty rough.