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

Feature Request: Allow to change default variable scope #12199

Closed
Tragen opened this issue Mar 24, 2020 · 10 comments
Closed

Feature Request: Allow to change default variable scope #12199

Tragen opened this issue Mar 24, 2020 · 10 comments
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered. WG-Language parser, language semantics

Comments

@Tragen
Copy link

Tragen commented Mar 24, 2020

Currently, the default variable scope allows variables to be accessed in child scopes.
I always had a lot of problems with this in bigger scripts.
It would be nice to have an option, e.g. at the beginning of a script, which tells PS
that the default scope should be private for this script.

@Tragen Tragen added the Issue-Enhancement the issue is more of a feature request than a bug label Mar 24, 2020
@Halkcyon
Copy link

That is the purpose of declaring your variables with scope, .e.g, $private:myVar

@vexx32
Copy link
Collaborator

vexx32 commented Mar 25, 2020

Correct. I believe the request is to have a way in the script to set such a scope to be the default for all variables in that scope, so that when creating an unscoped variable it is automatically assigned the private scope.

@iSazonov iSazonov added WG-Language parser, language semantics Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a and removed Issue-Enhancement the issue is more of a feature request than a bug labels Mar 25, 2020
@Tragen
Copy link
Author

Tragen commented Mar 25, 2020

Yes, exactly. When you have very long scripts, it's very ugly to prefix all variables with private

@mklement0
Copy link
Contributor

mklement0 commented Mar 29, 2020

PowerShell's dynamic scoping is a problem in other areas too, notably with not wanting Set-StrictMode settings to apply to child scopes ((third-party) code is often written with the assumption that no strict mode is in effect, and one going into effect in the caller's scope can break the code - see the (languishing) lexical strict-mode RFC at PowerShell/PowerShell-RFC#7

For more robust code, lexically scoped variables, or their emulation with the $private: scope modifier, but without the latter's ceremony would indeed be helpful, but requires an opt-in mechanism so as not to break backward compatibility.

Conceivably, a new using statement could using be introduced that makes all local variables private by default, along the lines of using private variables; however, this implies that the option is only available at the file level, not to individual functions. [update: also, the effect would have to be lexically scoped, which the current using statements are not].

@vexx32
Copy link
Collaborator

vexx32 commented Mar 29, 2020

Not a bad suggestion, @mklement0. For myself, I was imagining introducing a preference variable; e.g., $PSDefaultVariableScope = 'Private'

@mklement0
Copy link
Contributor

mklement0 commented Mar 29, 2020

The problem with preference variables is again their dynamic scoping - child scopes that weren't written with all-private variables in mind could break.

I suggested using under the mistaken assumption was that they are lexically scoped to the enclosing script / script module, but as it turns out they're not, not even in the using namespace case.

I suppose something like $private:PSDefaultVariableScope = 'Private' would work in a given scope, come to think of it, but that is obscure.

@iSazonov iSazonov added the Resolution-Answered The question is answered. label May 8, 2020
@Tragen
Copy link
Author

Tragen commented May 9, 2020

This isn't answered yet

@ghost
Copy link

ghost commented May 11, 2020

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

@ghost ghost closed this as completed May 11, 2020
@fabienfl-orc
Copy link

@iSazonov What is the answered resolution ?

@iSazonov
Copy link
Collaborator

iSazonov commented Oct 5, 2021

@fabienfl-orc PowerShell is well-designed language for scripting. :-) Later classes was introduced in PowerShell to address "programming" scenarios.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question ideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a Resolution-Answered The question is answered. WG-Language parser, language semantics
Projects
None yet
Development

No branches or pull requests

6 participants