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

Not implemented Empty when destructuring an array as an object in markup #6609

Closed
MarkTanashchuk opened this issue Aug 2, 2021 · 19 comments
Closed
Labels
bug compiler Changes relating to the compiler

Comments

@MarkTanashchuk
Copy link

MarkTanashchuk commented Aug 2, 2021

Describe the problem

At the moment, when I destructing array in this way in the markup:

{#each ["one", "two", "three"] as { 0: first, [arr.length - 1]: last }}
	<div>{first}</div>
	<br>
	<div>{last}</div>
{/each}

// UPD: This doesn't work either
//  {#each [["one", "two", "three"]] as { 0: first, [arr.length - 1]: last }}
//	  <div>{first}</div>
//    <br>
//	  <div>{last}</div>
//  {/each}

The compiler gives an error Not implemented Empty
However, this kind of destructuring is 100% valid js as you can see in REPL

Describe the proposed solution

I wish it would work in markup ☺️

Alternatives considered

Importance

would make my life easier

@MarkTanashchuk
Copy link
Author

The example in Repl is a bit inappropriate, but the full benefit of such destructuring can be felt when you need to get, for example, the fifth, tenth and fifteenth element of the array and assign it to variables.
This avoids the need to write cumbersome destructuring with commas like

const [,,,,five,,,,,ten,,,,,fifteen] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
                 ^ easy to make a mistake and put the wrong number of commas

@MarkTanashchuk
Copy link
Author

Much better IMO
const { 4: five, 9: ten, 14: fifteen } = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];

@Mlocik97
Copy link
Contributor

Mlocik97 commented Aug 2, 2021

that's invalid code

in {#each arr as item, idx} item is just one item, not list/array, You are destructing. What You want to do will be possible with:
https://github.com/sveltejs/rfcs/blob/master/text/0000-markup-constants.md

@MarkTanashchuk
Copy link
Author

MarkTanashchuk commented Aug 2, 2021

that's invalid code

That is valid code. Please check out repl with js example

In the case of svelte markup ATM of course invalid, but it's completely valid javascript

@Mlocik97
Copy link
Contributor

Mlocik97 commented Aug 2, 2021

no

{#each arr as { 0: firstElem, [arr.length - 1]: lastElem }}

is invalid... beucase what You are doing is { 0: firstElem, [arr.length - 1]: lastElem } = arr[idx]

where arr[idx] is not array, but only one item of array.

@MarkTanashchuk
Copy link
Author

MarkTanashchuk commented Aug 2, 2021

Please check out this link if you want to investigate possibilities of javascript

@MarkTanashchuk
Copy link
Author

MarkTanashchuk commented Aug 2, 2021

I think that if svelte imitates javascript in this respect, then it is a proper extension of the idea that has its purpose

Link to doc
You can use each blocks to iterate over 'any' array or array-like value — that is, any object with a length property.
and
You can freely use destructuring and rest patterns in each blocks.

@MarkTanashchuk
Copy link
Author

Although it's not suitable for each because of the disposability

@MarkTanashchuk
Copy link
Author

I think that each which iterates once is better than the Not implemented Empty error, so I opened an issue.
However, in this context, it can be used for the wrong purposes

@MarkTanashchuk
Copy link
Author

Therefore, there is no point in continuing the discussion of issue

@Mlocik97
Copy link
Contributor

Mlocik97 commented Aug 2, 2021

Your each was not iterating once, but 3 times.

@MarkTanashchuk
Copy link
Author

MarkTanashchuk commented Aug 2, 2021

The destructuring is done in one iteration, and when the entire array is disposed of across all of its indexes, as specified

I think that each which iterates once is better than the Not implemented Empty error, so I opened an issue

I would expect it to be executed once(instead of error).
However, svelte's each handles array elements by index, so yes, it iterates three times now

@MarkTanashchuk
Copy link
Author

The only question that remains is whether the Not implemented Empty error when using this syntax is ok

@Mlocik97
Copy link
Contributor

Mlocik97 commented Aug 2, 2021

{#each} is roughly equivalent to JS Array.prototype.forEach() (even tho, in real it compile to classic for() )

Your code was incorrect in way of expectation of what it should do... but I partially agree, syntax was correct, because item (string) was destructable, where it should write 3 time first and last symbol/character in string. So practically this is bug, but it's different bug than that You described.

@MarkTanashchuk
Copy link
Author

MarkTanashchuk commented Aug 3, 2021

Reopen. this destructuring doesn't work even with "valid" iteration

@MarkTanashchuk MarkTanashchuk reopened this Aug 3, 2021
@MarkTanashchuk
Copy link
Author

Error is the same

@Mlocik97
Copy link
Contributor

Mlocik97 commented Aug 3, 2021

I tried to add it to () and it's giving error Expected identifier or destructure pattern (9:14). So yes, this is BUG.

@Conduitry Conduitry added bug compiler Changes relating to the compiler labels Oct 19, 2022
@ngtr6788
Copy link
Contributor

ngtr6788 commented Mar 7, 2023

I have made a pull request #8357 that allows de-structuring objects with literal properties. For now, I do not know how best to fix the computed properties yet I have made another PR that deals with computed properties #8386.

@Conduitry
Copy link
Member

This should be fixed in 3.57.0 - https://svelte.dev/repl/d1f832a12ca1455b94cd87f4ef5c1196?version=3.57.0

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

No branches or pull requests

4 participants