bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: zsh style associative array assignment bug


From: Jesse Hathaway
Subject: Re: zsh style associative array assignment bug
Date: Mon, 5 Apr 2021 09:09:04 -0500

On Mon, Mar 29, 2021 at 4:18 PM Chet Ramey <chet.ramey@case.edu> wrote:
> If you look at
>
> a=( k1 v1 k2 v2 k3 v3)
>
> as more or less syntactic sugar for
>
> a=( [k1]=v1 [k2]=v2 [k3]=v3 )
>
> it's reasonable that
>
> a=( k1 v1 k2 )
>
> is equivalent to
>
> a=( [k1]=v1 [k2]= ). And that's what bash does.

This equivalence, though it is reasonable, creates a special case for the last
value in an array which imposes a greater cognitive burden on the programmer.
The programmer needs to be aware that omitting the last value will create an
empty value rather than emitting an error. Given that you need quotes to create
an empty value for any other element but the last:

  $ a=( k1 "" k2 v2 k3)
  $ declare -p a
  declare -A a=([k1]="" [k2]="v2" [k3]="" )

I would prefer the consistency and readability of always requiring an even
number of arguments:

  $ a=( k1 "" k2 v2 k3 "")
  $ declare -p a
  declare -A a=([k1]="" [k2]="v2" [k3]="" )

In general I find that fewer surprises in a language lead to easier to grok
code.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]