Fixed array.flat[Map] on [[]] (fixes #719).#737
Fixed array.flat[Map] on [[]] (fixes #719).#737Perryvw merged 5 commits intoTypeScriptToLua:masterfrom radekmie:fixed-array-flat-empty
Conversation
Perryvw
left a comment
There was a problem hiding this comment.
I have a feeling simply removing this check is not the answer here, I suggested 2 testcases that will either break your solution or help prove it is sound.
|
I've added these tests. Of course, they are failing. I'm just thinking, what is better to do here:
|
|
The problem is there is not really a way to distinguish an empty table from an empty object in lua (they are both translated to |
|
Alright, we discussed it and came to the following conclusion: For now we would be fine if the way you solve this is by assuming that if an object has no keys, it is an array. This is not entirely correct, but there is no straightforward (or even a complex one) way to deal with this perfectly, so we would rather go for the very simple non-perfect fix than to go for a very complex non-perfect fix. So then the check could indeed be modified as I suggested above by adding a special case to the if statement. Please add a comment to the LuaLib explaining what the reasoning behind |
|
It didn't help as ({ next(value, nil) }) == niland not, as expected, to next(value, nil) == nilAny idea why? EDIT: It's a more general problem, as |
|
That sounds very strange, can you just push your changes so I can have a look? |
|
It seems to be caused by EDIT: All tests pass even without this directive. |
|
That does explain the behavior. This turned out a bit more complicated than I expected. To get rid of the wrapping we need a bit of typing. I feel like there must be a better way but for now you can put this in declarations/global.d.ts: // Override next declaration so we can omit extra return values
type NextEmptyCheck = (this: void, table: any, index: undefined) => unknown | undefined;Then you change the check to if (type(value) === "table" && (1 in value || (next as NextEmptyCheck)(value, undefined) === undefined)) {Edit: oops, accidentally added some incorrect stuff to the NextEmptyCheck type, removed it now. |
|
I used In any case we should keep it consistent and use one of the methods for an isArray workaround across lualib. |
|
I also thought about that at first, but 👍 for a general EDIT: Maybe use |
|
I agree @lolleko, maybe we implement Array.isArray in the lualib and add some lualib dependencies? We can discuss it a bit more. That's a PR for another day. |
This change fixes a bug reported in #719: empty array elements (e.g.
[[]]) were incorrectly handled byarray.flatandarray.flatMap.