[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: sunday afternoon whishlist
- From: steve donovan <steve.j.donovan@...>
- Date: Mon, 10 May 2010 11:24:15 +0200
2010/5/9 Tony Finch <dot@dotat.at>:
> Perl's handling of nonexistent indices is more complicated than that.
> If you write $x->{y}->{z} (Perl for x.y.z) then $x-{y} is autovivified
> into a reference to a hash. This also applies to array references.
Autovivification also solves the inverse problem, when you are writing
things like t.a.b.c = 'value'; it would create the intermediate
tables if they didn't exist already.
Setting the metatable for nil is a cute hack but like all global
metatable hacks it affects everything, which is ok for small-ish
systems where you control everything.
BTW, there is a little inconsistency; tables do not have a default
metatable. If this were so, then another category of cute global
hacks becomes possible. ;)
While we're talking about wishlists, it would be useful if there were
a __methindex metamethod as well as __index, so that the lookup in
obj:name() does not have to be the same as obj.name/obj['name']. (This
is not a new suggestion, but IHMO a useful one to keep foregrounded.)
The obvious gain is able to define a Map class where you don't get
false positives (i.e. map['update'] is always true if there is a
Map.update method) but it's also useful for a List class: one can then
efficiently implement ls[-1] like Python without having to catch
everything using __index.
steve d.