(I assume this is a continuation of the discussion in #bash on Libera.chat
yesterday?)

The primary use of `set -u` is to detect misspelled variable names, and
misspelled keys for associative arrays seems like a reasonable extension of
that, if you assume that they're in some sense a fixed list, like the fixed
fields in a C struct.

However, Bash's associative arrays more closely resemble Perl hashes, PHP
arrays, Python dicts, JS objects, etc, where "missing" entities are normal
and expectable. At least some - maybe most - of the time they fulfill roles
more similar to these open dynamic structures than to traditional fixed
structs, and treating "unset" as a fatal error is not the experience that
programmers have using other scripting languages, and is therefore, I would
argue, not something that should be added to the Shell, and certainly not
as a quiet change to the existing `set -u`.

This leads to the conclusion that the fail-if-unset behaviour should be
specified on a per-array basis, which means it should be part of `declare`
and `local` rather than a global through `set` or `shopt`.

If we're heading down this route I have a bunch of other things that
overdue to be fixed, including:

1. ${array[@]:start:count} can under some circumstances return elements
with indeces >= start+count, completely contravening expectations from
other programming languages. There should be a `declare` or `local` option
to fix that, and/or a shopt setting.
2. ${#array[@]} gives the number of elements rather than the last index
(minus 1). Being able to declare an array as non-sparse (so that all gaps
"exist" with some default value) would fix this.

-Martin

Reply via email to