lua-users home
lua-l archive

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


Anton:

On Sun, Mar 1, 2020 at 4:28 PM Anton Jordaan <anton.jordaan@sylvean.com> wrote:
> >> Perl has auto-vivification and it creates way more problems than it
> >> solves. Also agree this should not be part of the language and the
> >> error when trying to index a null value is a good thing.
> I am not very familiar with Perl, but AFAIK, Perl's auto-vivification
> automatically creates array elements even when just *reading* from the
> array.

No, it does not, it just returns undef:

folarte@7of9:~$ perl -e '@x=(1); for(4..6) { print $x[$_]//"x"; }
print " $#x  $^V\n";'
xxx 0  v5.28.1

( For the uninitiated, this creates a list with a single element, 1,
then reads index 4,5,6 printing their content or x if it is undefined,
then prints the index of the last element of the ( 0 based ) array and
the interpreter version ).

And, AFAIK, it's been doing that since the eighties.

It does a similar thing on dictionaries:

folarte@7of9:~$ perl -e '%x=(a => 1); print $x{b}//"x"; print %x,"\n";'
xa1

But unlike lua it stores undef ( the more similar thing to nil it has ):
folarte@7of9:~$ perl -w -e '%x=(a => 1); $x{b}=undef; print %x,"\n";'
Use of uninitialized value $x{"b"} in print at -e line 1.
a1b

And autocreates intermediate slots, as it has lists separated from maps:

folarte@7of9:~$ perl -w -e '@x=(0); $x[3]=3; print @x," $#x\n";'
Use of uninitialized value in print at -e line 1.
Use of uninitialized value in print at -e line 1.
03 3
( note the double warning due to elements 1 and 2 being on the list
but undefined ).

But autovivification can sometimes create arrays/tables. I think it is
when you index an unitialized scalar variable.

To add some opinion, adding a safe navigation operator seems right,
although against keeping lua a simple language, and preserves back
compatibility, but changing the semantics of regular indexing of plain
tables does not seem right to me.

Francisco Olarte.