Why Strings as Classes?

Benji Smith dlanguage at benjismith.net
Wed Aug 27 10:14:04 PDT 2008


Dee Girl wrote:
> Michiel Helvensteijn Wrote:
>> That's simple. a[i] looks much nicer than a.nth(i).
> 
> It is not nicer. It is more deceiving (correct spell?). If you look at code it looks like array code.
> 
> foreach (i; 0 .. a.length)
> {
>     a[i] += 1;
> }
> 
> For array works nice. But for list it is terrible! Many operations for incrementing only small list.

Well, that's what you get with operator overloading.

The same thing could be said for "+" or "-". They're inherently 
deceiving, because they look like builtin operations on primitive data 
types.

For expensive operations (like performing division on an 
unlimited-precision decimal object), should the author of the code use 
"opDiv" or should he implement a separate "divide" function?

Forget opIndex for a moment, and ask the more general question about all 
overloaded operators. Should they imply any sort of asymptotic 
complexity guarantee?

Personally, I don't think so.

I don't like "nth".

I'd rather use the opIndex. And if I'm using a linked list, I'll be 
aware of the fact that it'll exhibit linear-time indexing, and I'll be 
cautious about which algorithms to use.

--benji



More information about the Digitalmars-d mailing list