jmoenig / Snap

a visual programming language inspired by Scratch
http://snap.berkeley.edu
GNU Affero General Public License v3.0
1.48k stars 739 forks source link

Script variables weirdness - declaration collision and access to them from outer scope #464

Closed studej closed 10 years ago

studej commented 10 years ago

Is that intention that I can declare new script variable by using the same indentifier in the same or nested scope? Imho following example should not work and should show error. script_variables2

There is an obvious collision.


@jmoenig commented https://github.com/jmoenig/Snap--Build-Your-Own-Blocks/issues/157

Unlike BYOB Snap has a uniform scope, i.e. you can use any variable setter or getter block anywhere and it'll simply look up the value for its variable name in the current scope, so there is no need for scope contrast anymore.

It is true that I can check which variable is available in the context: script_variables1 (variable "a" is not accesible and also should not exist anymore after we left body of if block)

...but you can do this: script_variables

How is it possible that script variable declared in nested scope is available to outer scope? It should not exists at all.

jmoenig commented 10 years ago

the primitives (IF) are special cases. You can declare a script variable inside an IF-block. I'm not sure whether I want to address this...

studej commented 10 years ago

Hmm yes I see. I created custom "if block" and then variable was not accessible. From your post @jmoenig it seems that user should not be able to declare script variable in IF block, did I understand it? So script variable is a variable only on level of script/custom or anonymous block? I was thinking that script variable == local variable.

jmoenig commented 10 years ago

No, it's okay to declare script vars inside primitive loops and conditionals, but, in that case they'll be accessible in the whole "script", not just within the enclosing C-shaped slot. Script vars are temporary variables at the script level.

brianharvey commented 10 years ago

Apparently we're supposed to think of SCRIPT VARIABLES as something executable, not as a declaration. "Create a new variable right now with the current script as its scope." (This is in answer to the first picture above.) That's what Berkeley Logo does, so it must be right! :-)

cycomachead commented 10 years ago

"Create a new variable right now with the current script as its scope." (This is in answer to the first picture above.) That's what Berkeley Logo does, so it must be right! :-)

Yes, this makes sense to "current scope" doesn't always make sense in the context of a loop / conditional. There's plenty of cases where block level scoping is insanely convenient.

Not saying Snap! is wrong, and I agree with keeping it simple, but there are other options.