[Issue 3474] PATCH: Implement opDollar for struct and class indexing operations

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Nov 6 06:43:48 PST 2009


http://d.puremagic.com/issues/show_bug.cgi?id=3474



--- Comment #6 from Stewart Gordon <smjg at iname.com> 2009-11-06 06:43:47 PST ---
(In reply to comment #5)
>> I agree that "opDollar" is a bad choice of name, but so is 
>> "length".  AIUI the point is for it to represent the end (or one 
>> past the end) of the array, rather than length.  They correspond 
>> only in the case of 0-based arrays.
> 
> It's elready defined semantics.  $ means length.

It's defined only on built-in linear arrays:

http://www.digitalmars.com/d/2.0/arrays.html
"Within the [ ] of a static or a dynamic array, the variable length  is
implicitly declared and set to the length of the array. The symbol $ can also
be so used."

Try it on AAs and see for yourself.  (Sidenote: I thought the implicitly
declared length was deprecated.)

Moreover, FAIK, it may have been described this way only for simplicity, as
Walter never expected to make it overloadable for custom types where "length"
and "index one after the end" are distinct.

> If you want different semantics, use different means to express it.  
> Objects in container can be numbered from 1, numbering can have 
> gaps, so $-1 can be meaningless,

You're giving me exactly the already-known reasons $ ought to be defined along
the lines of "end" rather than "length".  It's probably how most D programmers
think of it, because the main use for $ is to index relative to the end of an
array.  And so, if we start encouraging programmers to make $ mean the number
of elements in a non-0-based array, sparse array or whatever, it'll get
confusing.

I certainly can't see any reason for keeping it as "length".  And it would
break 0% of existing code to change it now.

As for "numbering can have gaps", there are at least two cases to consider:
- indexes are spaced by a constant interval k, IWC [$-k] feels a natural way of
referring to the final element analogous to [$-1] for standard arrays
- indexes are (or may be) irregularly spaced, IWC what $ should mean, if
anything, is harder to define

> or entire collection can be unordered like hashtable, so the last 
> element can be meaningless, or keytype can be of complex type, 
> having nothing in common with numbers, you just use different means 
> to work with it.

IWC you would probably not define $ at all.  If there's any problem with that,
I can't see it....

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list