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

global variables dropdown should check for all blocks instead of only top blocks #22

Open
lukeplays33 opened this issue Jan 16, 2022 · 3 comments
Labels
enhancement New feature or request lexical variables Use this tag for the lexical variable plugin

Comments

@lukeplays33
Copy link

Hi,

i have created a block that creates an element and then returns its ID as an global variable.

However this works fine unless the block is inside of an top block see video: https://drive.google.com/file/d/1zgOjPKQKnw8HbemuX8CskOFd0HbikZAt/view?usp=sharing

Thanks

@mark-friedman
Copy link
Collaborator

This is a bit of an unusual case. In most languages, global variables are created at the top-level of the program. This is the general practice because then you can clearly see where all the global variables are defined and initialized. Otherwise, they may be hidden in some deeply nested block. Moreover, it's possible that in some execution paths some global variable might be defined, but in others it won't be, which makes it even more difficult to reason about them and to understand and debug the program.

JavaScript is an outlier in how var works but in modern JavaScript programming it is considered bad practice to use var in non-top-level contexts.

Therefore, I would suggest that you switch to having your block introduce the variable as lexically scoped. Then the user can define their own top-level global variable and assign to it from within your block.

@mark-friedman mark-friedman added enhancement New feature or request lexical variables Use this tag for the lexical variable plugin labels Jan 16, 2022
@lukeplays33
Copy link
Author

lukeplays33 commented Jan 16, 2022 via email

@mark-friedman
Copy link
Collaborator

...i thought to make it more easier for the user to Use the ID without creating An extra variable outside of the block.

Sure, I get that, but I think that the risks that I outlined generally outweigh that advantage for anything but the very simplest of programs. Even top-level global variables tend to get overused and unwieldy in many block programs.

That said, your feature request is not completely unreasonable. Unfortunately, however, I'm afraid that it is unlikely to get implemented in the near future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request lexical variables Use this tag for the lexical variable plugin
Projects
None yet
Development

No branches or pull requests

2 participants