On 2008-Jun-1, at 11:20 am, Jon Lang wrote:
David Green wrote:
I thought it already did, but apparently it's something that we
discussed
that didn't actually make it into S09. I agree that .[] should
apply to
hashes just as .{} can apply to arrays. The hashes don't even need
to be
sorted -- %h[$n] would basically be a shorter way of saying
@(%h.values)[$n], in whatever order .values would give you.
I believe that the order that .values (or is that :v?) would give
you is determined by .iterator - which, if I'm understanding things
correctly, means that any use of :v, or :k, :p, or :kv, for that
matter, would autosort the hash (specifically, its keys).
Or am I reading too much into autosorting?
No, that's my understanding too, with the observation that it doesn't
matter whether there's any sorting going on (so the order might be
arbitrary -- as in your next point).
Bear in mind that keys are not necessarily sortable, let alone
autosorted. For instance, consider a hash that stores values keyed
by complex numbers: since there's no way to determine .before
or .after when comparing two complex numbers, there's no way to sort
them - which necessarily means that the order of :v is arbitrary,
making %h[0] arbitrary as well.
Aha -- I guess you're thinking in terms of "autosorting" that can be
turned on to return the values ordered by < and >. But .iterator can
do whatever it wants, which in the default case is presumably to walk
a hash-table (technically not an arbitrary order, although it might as
well be as far as a normal user is concerned). Or you could
make .iterator return values sorted by alphabetical string-
representation (which would work for complex numbers), or you could
make it work by using < and > (which wouldn't work with complexes).
Which doesn't contradict anything you've said of course -- but I'm OK
with arbitrary orders. If %h[0] is arbitrary, that's fine.
This is why I was suggesting that it be limited to autosorted
hashes: it's analogous to how @a{'x'} is only accessible if you've
actually defined "keys" (technically, user-defined indices) for @a.
Do you see that as a psychological aid? That is, if the order is
arbitrary, %h[0] makes sense technically, but may be misleading to a
programmer who reads too much into it, and starts thinking that the
order is meaningful.
My feeling is that it's fine for all hashes to support []-indexing.
But if the order isn't important, then you probably wouldn't need to
use [] in the first place (you'd use "for %h:v", etc.)... so maybe it
should be limited. Hm.
-David
P.S. Everywhere I said < and > I really meant .before and .after. =P