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

Wrong order of execution for reactive variable #4371

Closed
milkbump opened this issue Feb 5, 2020 · 5 comments
Closed

Wrong order of execution for reactive variable #4371

milkbump opened this issue Feb 5, 2020 · 5 comments
Labels
awaiting submitter needs a reproduction, or clarification bug temp-stale
Milestone

Comments

@milkbump
Copy link

milkbump commented Feb 5, 2020

When I assign a value from a standard variable to a reactive variable, accessing the reactive variable afterwards results in an "uninitialized error".

For example:

let val = "onion";

$: gold = val;

console.log("gold", gold);

Generates:

let val = "onion";
function instance($$self) {
	console.log("gold", gold);    // this comes before
	let gold;
	$: gold = val;
	return [];
}

Which results in a Cannot access 'gold' before initialization error. I would expect it to generate:

let val = "onion";
function instance($$self) {
	let gold;
	$: gold = val;
        console.log("gold", val);    // this comes after
	return [];
}

See Svelte REPL repro

@tivac
Copy link
Contributor

tivac commented Feb 5, 2020

$: console.log("gold", gold);

will do what you want.

@milkbump
Copy link
Author

milkbump commented Feb 5, 2020

Thanks. I figured as much. I still think that the current behaviour is unexpected and probably a bug.

@Conduitry
Copy link
Member

I'm undecided about what to do here. One option would be to just put the auto declarations from reactive variables earlier, but that might not be too helpful, as I think the non-reactive references to them would just get undefined. Perhaps it should be a compiler error to refer to an auto declared reactive variable from the top level of the <script>? Are there other situations we need to worry about? What happens when someone synchronously calls a function from the top level, and that refers to an auto declared variable?

@Conduitry Conduitry added bug awaiting submitter needs a reproduction, or clarification labels Mar 9, 2020
@dummdidumm
Copy link
Member

This no longer breaks in the most recent version and instead prints undefined

@dummdidumm
Copy link
Member

$: blocks are executed at the end of initialization, but it isn't obvious. Svelte 5 will fix this through the runes API. In runes mode, it's a function through which it's more obvious that they don't fire immediately / you don't expect them to be executed immediately.

@dummdidumm dummdidumm added this to the 5.x milestone Nov 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting submitter needs a reproduction, or clarification bug temp-stale
Projects
None yet
Development

No branches or pull requests

6 participants