[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: A Question of Notation
- From: steve donovan <steve.j.donovan@...>
- Date: Sun, 19 Dec 2010 10:20:06 +0200
Hi all,
I'm revising Penlight for a new release soon, and started
experimenting with extending the List class.
By making __index a function, it's easy enough to support negative
indices, so that ls[-1] == ls[#ls] - (although I still want to
quantify the impact on method dispatch times.) This is consistent with
other Lua usages.
But consider the slice notation that was discussed recently. The
problem is that ls[{1,4,6]}] says 'index by array' to me (this is a
common idiom in array-oriented languages). This can be given
equivalent meaning on the left-hand side, although this is probably
not so useful. If Range(1,3) == {1,2,3} then ls[Range(1,3)] is in fact
the value of the slice from 1 to 3 - but this can't mean slice
assignment.
For slice assignment, I would need a special R type so that
ls[R(1,3)] = {10,20,30} replaces the slice from 1 to 3 with the given
table or sequence.
The interaction between array-indexing and slice-specification strikes
me as potentially confusing. So the question would be; are these
notational conveniences worth the possible confusion? I'm
particularly struck by the fact that ls[R(1,3)] and ls[Range(1,3)]
happen to be equivalent, but only on the right-hand side of an
assignment.
steve d.