Hacker News new | ask | show | jobs
by jiofih 1949 days ago
Autovivification was one of the most painful features I’ve had to live with - in a large codebase it completely erases trust on any kind of defined() check and breaks all sorts of things unexpectedly.

Yet another horrible hack in Perl that for some reason is advocated for. Optional chaining / null propagation is a much, much better idea and shouldn’t have been any harder to implement.

2 comments

I see how that can become a problem but I think the pros outweigh the cons. Maybe the problem is because of a "very large" codebase, my general hot take is that dynamic languages like Perl and Python should be used primarily for scripts. The lack of static typing makes dealing with very large codebases rather painful in any case, in my experience.

Null propagation is nice too, but it doesn't address all the uses cases of autovivification when you have, say, a hash table of arrays and you want to insert a new entry in an array, creating it if it doesn't exist. In python you have to use setdefault which I always found clunky.

FWIW, that was one of the things that Larry fixed in Perl 6 (now the Raku Programming Language https://raku.org #rakulang).

You can bind to a non-existing hash element (even multiple levels deep) and it won't exist until you actually assign to it:

    my %h;
    my $c := %h<a><b><c>;
    say %h<a>:exists;  # False
    $c = 42;
    say %h<a>:exists;  # True