Why Strings as Classes?

Chris R. Miller lordSaurontheGreat at gmail.com
Mon Aug 25 19:11:51 PDT 2008


Benji Smith wrote:
> BCS wrote:
>> Reply to Benji,
>>
>>> BCS wrote:
>>>
>>>> Ditto, D is a *systems language* It's *supposed* to have access to
>>>> the lowest level representation and build stuff on top of that
>>>>
>>> But in this "systems language", it's a O(n) operation to get the nth
>>> character from a string, to slice a string based on character offsets,
>>> or to determine the number of characters in the string.
>>>
>>> I'd gladly pay the price of a single interface vtable lookup to turn
>>> all of those into O(1) operations.
>>>
>>> --benji
>>>
>>
>> Then borrow, buy, steal or build a class that does that /on top of the
>> D arrays/
>>
>> No one has said that this should not be available, just that it should
>> not /replace/ what is available
> 
> The point is that the new string class would be incompatible with the
> *hundreds* of existing functions that process character arrays.
> 
> Why don't strings qualify for polymorphism?

-------------------------------------------
wchar[] foo="text"w;

int indexOf(char[] str,char ch){
    foreach(int idx,char c;str)
        if(c==ch) return idx;
    return -1;
}

void main() {
    assert(indexOf(foo, 'x')==2);
}
-------------------------------------------

If that does compile, it shouldn't.  The best way to get that to work is
to use a template.  Templates can be annoying.  A String class could
simplify the different kinds of String inherent in D.  The String class
would (should) internally know what kind of String it is (wchar, char,
dchar) and to know how to mitigate those differences when operations are
called on it.

@Benji
If you want a String class, why don't you write one?  It's a fairly
simple task, even high-school CS students do it quite routinely in C++
(which is a lot more unwieldy for OOP than D is).

A very successful instance of Strings-as-objects is present in Java.
I'd suggest trying to duplicate that functionality.  Then you could
easily write wrappers on existing libraries to use the new String object.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 258 bytes
Desc: OpenPGP digital signature
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20080825/d681cd61/attachment.pgp>


More information about the Digitalmars-d mailing list