-
-
Notifications
You must be signed in to change notification settings - Fork 164
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
[builtin/assign] Do not remove existing arrays by 'declare -a array'. #731
[builtin/assign] Do not remove existing arrays by 'declare -a array'. #731
Conversation
Hm I'm curious where does ble.sh need to say I tend to use "declare/local" to initialize once, and then mutate it later with Same with I feel like this falls mostly under: |
Actually, a problem occurred with associative arrays in function user-setting {
local dict_name=${1}_dict
declare -gA "$dict_name"
eval "$dict_name[\$2]=\$3"
}
user-setting hello aaa bbb Of course, maybe one can rewrite it in the form like
No, this PR does not affect when the type of existing variable changes. This PR only changes the behavior when the type is unchanged, i.e., when Note that in Bash, the conversion from scalars to arrays or associative arrays are allowed, but the opposite direction and the conversion between associative arrays and arrays are not allowed. In the allowed cases, the value is always preserved in some way. When a scalar is converted to an array (associative array), the original value becomes the first element (the value for the key
The above case will not be affected by the PR because it changes the type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I see. Yeah I guess it makes sense. I kind of don't like how it now depends on the old value, but I can't think of a better way to do it.
I guess it could be behind an option. But the reason I have var
in Oil is so we can design our own semantics, so declare
can retain compatible semantics.
I think it could be cleaner if _ReconcileTypes
now depends on the old values. But since the tests pass we can leave it for now and refactor later.
Thank you! |
When
declare -a array
is performed on existing arrays, the array becomes empty. In this PR, the arrays are initialized only when the variable contains another type of value. This also fixes the same problem for associative arrays.