A new class -->String

David B. Held dheld at codelogicconsulting.com
Thu Apr 5 01:03:13 PDT 2007


Jari-Matti Mäkelä wrote:
> David B. Held wrote:
>> jinheking wrote:
>>> I want to make a Class like java's String Class.
>>> [...]
>> Well, I believe the intention is that when 'invariant' gets added to the
>> language, we will get string thusly:
>>
>> alias invariant char[] string;
>>
>> Note that this leaves out all of the member functions that one expects
>> from String.  However, with immutable strings, it is perfectly safe
>> (and, perhaps, preferable) to implement all that functionality as free
>> functions.  If there were a way to switch between method call and free
>> function syntax, then this distinction would become moot (and maybe we
>> will get that as well...).
> 
> D has that already :)
> 
> char[] moo;
> 
> void doSomething(char[] baa) {
> 	...
> }
> 
> void main() {
> 	moo.doSomething();
> }

Well, it isn't general:

void foo(int x) { int y = x; }

unittest
{
	int z = 5;
	z.foo();
}

test.d(43): Error: no property 'foo' for type 'int'
test.d(43): Error: function expected before (), not 1 of type int

Dave



More information about the Digitalmars-d mailing list