opCaret to complement opDollar when specifying slices
Dario Schiavon
dario.schiavon at gmail.com
Sat Jun 2 04:49:16 PDT 2012
Hi,
I just read some old threads about opDollar and the wish to have
it work for non zero-based arrays, arrays with gaps, associative
arrays with non-numerical indices, and so on. It was suggested to
define opDollar as the end of the array rather than the length
(and perhaps rename opDollar to opEnd to reflect this
interpretation), so that collection[someIndex .. $] would
consistently refer to a slice from someIndex to the end of the
collection (of course the keys must have a defined ordering for
it to make sense).
I'm just thinking, if we want to generalize slices for those
cases, shouldn't we have a symmetrical operator for the first
element of the array? Since the $ sign was evidently chosen to
parallel the regexp syntax, why don't we add ^ to refer to the
first element? This way, collection[^ .. $] would slice the
entire collection, just like collection[].
Until now, ^ is only used as a binary operator, so this addition
shouldn't lead to ambiguous syntax. It surely wouldn't be used as
often as the opDollar, so I understand if you oppose the idea,
but it would at least make the language a little more "complete".
More information about the Digitalmars-d
mailing list