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

local and global variables #63

Open
shadichy opened this issue Nov 24, 2022 · 6 comments
Open

local and global variables #63

shadichy opened this issue Nov 24, 2022 · 6 comments
Labels
enhancement New feature or request

Comments

@shadichy
Copy link

No description provided.

@daniel-sc
Copy link
Owner

@shadichy could you provide some example input together with the expected output?

@daniel-sc daniel-sc added the enhancement New feature or request label Nov 24, 2022
@shadichy
Copy link
Author

@daniel-sc

like

someFunc() {

  aGlobalVar=1

  local aLocalVar=1

}

should returns something like

:someFunc

set  aGlobalVar=1

setlocal
set aLocalVar=1
endlocal

exit /b 0

@daniel-sc
Copy link
Owner

@shadichy I see some problem with this approach (wrapping individual 'local' assignments with 'setlocal'/'endloacal'): When the variable is used in subsequent lines the value is gone - e.g. the following script

ECHO OFF

setlocal
set aLocalVar="hello"
echo "inside first setlocal: %aLocalVar%"
endlocal

echo "outside setlocal: %aLocalVar%"

setlocal
echo "inside second setlocal: %aLocalVar%"
endlocal

has the output

"inside first setlocal: "hello""
"outside setlocal: "
"inside second setlocal: "

And since 'local' and 'global' variables could occur in any order this is a problem - or did I miss something?

@shadichy
Copy link
Author

@daniel-sc that's how 'local' variables work :)
I think that when using local variables, any command that uses those vars should be wrapped inside setlocal and endlocal, and if any local var is returned globally, use something like this e.g:

endlocal & (
  set "GlobalVar1=%LocalVar1%"
  set "GlobalVar2=%LocalVar2%"
  set "GlobalVar3=%LocalVar3%"
)

@daniel-sc
Copy link
Owner

Yeah I understand. The point is, that a mere syntactically approach (as currently used) does not suffice to solve this. One would need to analyze all variables and their usages..

@shadichy
Copy link
Author

seems hard
Seems Microsoft has done really great in confusing their customers :)

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

No branches or pull requests

2 participants