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

Weird display with array reverse method #7295

Closed
tanangular opened this issue Feb 20, 2022 · 6 comments
Closed

Weird display with array reverse method #7295

tanangular opened this issue Feb 20, 2022 · 6 comments

Comments

@tanangular
Copy link

Describe the bug

I know that reverse() is array mutable method, but I'm not sure. Is this a bug or not?
Why is the display different between these 2 examples? (note the display of original array)

array reverse() in text fragment

Screenshot 2565-02-20 at 18 17 41

Compare with

weird array reverse in html block element
Screenshot 2565-02-20 at 18 22 12

Reproduction

as shown in the captured screen above

Logs

No response

System Info

System:
    OS: macOS 12.2.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 88.98 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 17.5.0 - ~/.nvm/versions/node/v17.5.0/bin/node
    npm: 8.4.1 - ~/.nvm/versions/node/v17.5.0/bin/npm
  Browsers:
    Chrome: 98.0.4758.102
    Chrome Canary: 101.0.4898.0
    Edge: 98.0.1108.43
    Firefox: 97.0
    Safari: 15.3
    Safari Technology Preview: 15.4

Severity

annoyance

@Prinzhorn
Copy link
Contributor

This has come up multiple times. You are mutating state inside the template expression, which is kind of undefined behavior. Something that in my opinion should straight up not compile in Svelte 4. You are relying on the order in which Svelte decides to do things, which is not necessarily from top to bottom because this is not a templating language but a living application. Svelte can do things in whatever order it thinks is most efficient. Someone with knowledge about internals can probably tell you exactly why you are seeing what you are seeing, which could actually be different between Svelte versions. In fact check out 3.0.0 https://svelte.dev/repl/2b54f8e14ae7455d8f653cc954d32c50?version=3.0.0

Related

#3793
#5897
#5893
#5460
#7229

@Conduitry
Copy link
Member

Yup, changing component state inline in the template is undefined behavior. Preventing this at compile time would be nice to do in Svelte 4 where possible, but we're not going to be able to catch cases like this where there's no visible assignment and instead the state change happens in a method that mutates state.

@brandonmcconnell
Copy link
Contributor

@Conduitry @Prinzhorn if we want to avoid arbitrarily supporting inline logic like this, what if Svelte added support for helper statements/blocks similar to {@const} like {@set} where the state for one or more stately variably could be redefined within template logic:

<script>
  let arr = [1, 2, 3];
  let index = 0;
</script>

{#each arr as elem, i}
  {@set index  } <!-- or {@set index  = 1} or {@set index = index 1} -->
  This {index === i ? 'works' : 'doesn\'t work'} when `i` equals {i}, where `index` equals {index}.
{/each}

@Conduitry
Copy link
Member

Giving it its own syntax doesn't resolve any of the concerns with how it should behave. {@set} would need to contend with everything that {} does now.

@Prinzhorn
Copy link
Contributor

That's still undefined behavior. When would @set execute? Svelte is a living document, this is not an old fashioned templating language that is executed top down. Your #each is alive. Changing arr might only update a subset of the items. Adding items to arr in your example would just keep growing index. I don't understand the use-case for something like this at all. It doesn't make sense to me outside of a "classic" templating language (it might make sense for a single SSR pass without hydration, but that's exotic).

If you think you have a use-case that Svelte does not support in a nice way and that doesn't already have an open or clsoed feature request, then please provide such a feature request. But in a more detailed way than your comment (the feature request issue template will guide you).

@brandonmcconnell
Copy link
Contributor

@Prinzhorn Of course. Just contributing to the discussion here. I see your point about the template being alive and the declarative vs. imperative conflicts something like {@set} could cause.

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

4 participants