-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Using expression containing Math.random() more than once wrongly assumes all instances should have same result #15096
Comments
@Rich-Harris Looks like a recent regression relating to the work you did on the compilation of the template? |
Ha, yep. Not totally sure what a good fix would be other than just reverting the duplicate expression detection, which would be a shame... either way a workaround in the meantime is to make a meaningless change to one of the expressions: -{(Math.random(i) * 100).toFixed(1)}
+{(0, Math.random(i) * 100).toFixed(1)} |
I'm torn about this. What sorts of practical situations - besides ones that eventually call If we can get away with it, I'd love to push people towards "all template expressions need to be pure, otherwise undefined behavior will happen" and "if you need to do something stateful, do it in your |
@Conduitry We could probably do this and document it for Svelte 6, but this might very well be a breaking change now. Whilst people shouldn't be doing side-effects in the template, there's no documentation saying this other than the error they'll get from mutating state, which only happens in runes mode. |
Maybe I'm rusty on the terms, but I think the correct terminology is "math.random() is a pure function, but non-deterministic". It is pure because it doesn't have side effects (unsure about which side effects Conduitry is referring to), but its return value is different on every call. UPDATE: Ah, ok. The internal state that gets seeded on every call. Gotcha. If my memory is serving me well here regarding terms, the issue is not about side effects, but about determinism, and sure, math.random() is probably not something that is likely seen in production, but there are other non-deterministic expressions, such as Date.now. |
|
Ah, it seems that the term "pure function" also refers to a "deterministic function", but not the other way around. 👍 |
For the issue at hand, however, the problem seems that the new algorithm assumes that all expressions are deterministic. Internal state changes don't really bother the algorithm in its current form, as I understand the matter. Since it is probably impossible to define and exclude every non-deterministic expression out there, I think the change will probably have to be rolled back. The workaround provided by Rich is solid, but it's not very maintainable. |
How about adding a per-component option to tell the compiler to disable Rich's new feature? |
Absolutely not |
Hello, I found a strange behavior :
Otherwise, although pure functions are preferable, It might be useful to be able to disable this detection in very specific cases like this one. A special template syntax seems appropriate to me. |
Describe the bug
An expression is used here twice, but because it contains Math.random() it shouldn't be assumed that the same result will occur every time it is used.
Reproduction
v5.19.0 (Desired outcome)
https://svelte.dev/playground/hello-world?version=5.19.0#H4sIAAAAAAAACo3PvW7DIBQF4FdBVFFwFNnJ0IW4kbJUXfoEdQdirgMqBgQ3PxbyuxeiVF06dP107jmQqBUjUE7fwBhHri4YSRhIjSAruqaDNhAp_0gUJ19yBbI_rg7e1_ECBosdRYS_vHcWwWKuoekJRK_IIQQxse1mU82d7bCV-kIiTgZelug8T-xdoKqDsNKNrCIrUqI1uld9A8m21bzYGRjwP8Hlvm1y_b6zqSnbZdC297FsZTiVJ3gXNWpnORnK6a7YUfRfp-DOVnISHnbVEhUn9XOA8Q4K9Enhr-T-tvmpz59HuCHlGM4wf87fUt4xEm4BAAA=
v5.19.1+ (Undesired outcome)
https://svelte.dev/playground/hello-world?version=5.19.1#H4sIAAAAAAAACo3PvW7DIBQF4FdBVFFwFNnJ0IW4kbJUXfoEdQdirgMqBgQ3PxbyuxeiVF06dP107jmQqBUjUE7fwBhHri4YSRhIjSAruqaDNhAp_0gUJ19yBbI_rg7e1_ECBosdRYS_vHcWwWKuoekJRK_IIQQxse1mU82d7bCV-kIiTgZelug8T-xdoKqDsNKNrCIrUqI1uld9A8m21bzYGRjwP8Hlvm1y_b6zqSnbZdC297FsZTiVJ3gXNWpnORnK6a7YUfRfp-DOVnISHnbVEhUn9XOA8Q4K9Enhr-T-tvmpz59HuCHlGM4wf87fUt4xEm4BAAA=
Logs
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: