Re: a == a[0] convention

2016-06-07 Thread Dan Douglas
On Thu, Jun 2, 2016 at 12:18 PM, Grisha Levit  wrote:
> I think all of the stuff here is fixed in the latest devel snapshot

Ah ok I tried this again. Yes this looks better now, thanks.

> there is definitely weirdness if you run the assignments as a typeset
> command.  I hadn't even tried the ref4 case, which gives the weirdest
> results when used with typeset/declare:
> https://groups.google.com/forum/#!topic/gnu.bash.bug/Z-lf3KEnZ34

That's what reminded me of this, but your example was definitely a bug
(regression since 4.3 at least). The ref4[0] one is just an odd
special case but I think it's probably easier to treat it as an error.



Re: a == a[0] convention

2016-06-02 Thread Grisha Levit
I think all of the stuff here is fixed in the latest devel snapshot, though
there is definitely weirdness if you run the assignments as a typeset
command.  I hadn't even tried the ref4 case, which gives the weirdest
results when used with typeset/declare:
https://groups.google.com/forum/#!topic/gnu.bash.bug/Z-lf3KEnZ34

bash -xc '
typeset -n ref1=a1 ref2=a2 ref3=a3[0] ref4=a4[0]
ref1=foo ref2[0]=foo ref3=foo ref4[0]=foo
echo "${ref1} ${ref2[0]} ${ref3} ${ref4[0]}"
typeset -p {ref,a}{1,2,3,4}'
+ typeset -n ref1=a1 ref2=a2 'ref3=a3[0]' 'ref4=a4[0]'
+ ref1=foo
+ ref2[0]=foo
+ ref3=foo
+ ref4[0]=foo
bash: line 2: `a4[0]': not a valid identifier
+ echo 'foo foo foo '
foo foo foo
+ typeset -p ref1 ref2 ref3 ref4 a1 a2 a3 a4
declare -n ref1="a1"
declare -n ref2="a2"
declare -n ref3="a3[0]"
declare -n ref4="a4[0]"
declare -- a1="foo"
declare -a a2=([0]="foo")
declare -a a3=([0]="foo")
bash: line 4: typeset: a4: not found


bash -c 'typeset -n ref[0]=foo'
bash: line 0: typeset: ref[0]: reference variable cannot be an array


a == a[0] convention

2016-06-01 Thread Dan Douglas
This is possibly relevant to some of Grisha's observations. First,
declaring or assigning to a variable with a subscript. I think I would
prefer these to be errors for various reasons. Admittedly there's an
argument for making one or both of these non-errors for declarations
without assignment for consistency with the current documentation, and
maybe even allowing the current behavior (no errors ever). I think
that leads to confusion though.

 $ bash -c 'typeset -n ref[0]=foo' # no error
 $ bash -c 'typeset -n ref[1]=foo' # no error

The other variations on this theme are if there are references to
"arrays" with or without a zero subscript. Bash example:

 $ bash -xc '
typeset -n ref1=a1 ref2=a2 ref3=a3[0] ref4=a4[0]
ref1=foo ref2[0]=foo ref3=foo ref4[0]=foo
echo "${ref1} ${ref2[0]} ${ref3} ${ref4[0]}"
typeset -p {ref,a}{1,2,3,4}'

+ typeset -n ref1=a1 ref2=a2 'ref3=a3[0]' 'ref4=a4[0]'
+ ref1=foo
+ ref2[0]=foo
+ ref3=foo
+ ref4[0]=foo
+ echo 'foo foo foo foo'
foo foo foo foo
+ typeset -p ref1 ref2 ref3 ref4 a1 a2 a3 a4
declare -n ref1="a1"
declare -a ref2=([0]="foo")
declare -n ref3="a3[0]"
declare -a ref4=([0]="foo")
declare -- a1="foo"
/home/ormaaj/doc/programs/bash-build/bash: line 4: typeset: a2: not found
declare -a a3=([0]="foo")
/home/ormaaj/doc/programs/bash-build/bash: line 4: typeset: a4: not found

I think the mksh result for that code is most like what bash is
probably going for. It mostly treats `ref[0]` as `ref` both when
assigning and dereferencing.

 $ mksh -xc '
typeset -n ref1=a1 ref2=a2 ref3=a3[0] ref4=a4[0]
ref1=foo ref2[0]=foo ref3=foo ref4[0]=foo
echo "${ref1} ${ref2[0]} ${ref3} ${ref4[0]}"
typeset -p {ref,a}{1,2,3,4}'
+ typeset -n 'ref1=a1' 'ref2=a2' 'ref3=a3[0]' 'ref4=a4[0]'
+ ref1=foo ref2[0]=foo ref3=foo ref4[0]=foo
+ echo 'foo foo foo foo'
foo foo foo foo
+ typeset -p ref1 ref2 ref3 ref4 a1 a2 a3 a4
typeset -n ref1=a1
typeset -n ref2=a2
typeset -n ref3='a3[0]'
typeset -n ref4='a4[0]'
typeset a1=foo
set -A a2
typeset a2[0]=foo
set -A a3
typeset a3[0]=foo
set -A a4
typeset a4[0]=foo

ksh93 also does that except ref4 which understandably assigns to a4[0][0].