Parameter Substitution on arrays
feat: Add an equivalent @or method for arrays. With this addition both arrays and string can have alternative default values. How would it work? An empty array would trigger the @or method to return the second argument instead of the first. It could work with array slicing, but it's forbidden to create empty slices of arrays as of right now, thus the method would never trigger and return the second argument.
BREAKING CHANGE: None that I know of.
perf: impact
performance none
usability increase
maintainability increase
code: input
let array = []
# Inline array in @or method
echo @or(@array [foo bar])
# single value
echo @or(@array baz)
# variable expansion
let default = foobar
echo @or(@array $default)
# method would not trigger
let array ++= faz
echo @or(@array $default)
expect: output
foo bar
baz
foobar
faz
reason: Strings already have this feature and arrays should to. Parameter substitution are very common in bash scripts and should be ported over fully.
context: This is a stripped down version of a script I have today. I have to add the default argument in a check when I could have written it like: "${@-status}" in bash. I can't demonstrate how I would use the @or method as I still depend on array slicing.
fn mix_status
if bool $(pamixer --get-mute)
echo "--%"
else
printf "%2s%%\n" $(pamixer --get-volume)
end
end
test $len(@args) = 1 && let args ++= "status"
for arg in @args[1..]
match $arg
case status; mix_status
case toggle-mute; pamixer --toggle-mute
case mute; pamixer --mute
case _; echo "Usage: $(basename @args[0]) [status|toggle-mute|mute] ... (default: status)"
end
end
behavior of bash
Here is bash solution to parameter substitution: https://tldp.org/LDP/abs/html/parameter-substitution.html