www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Extendable properties/methods

A good and large thread on the topic of revisited syntax for properties. 
How about an idea for adding methods and properties for *all* kinds of 
types, not just array types.

int indexOf(char[] haystack, char needle);
Can now be used as either indexOf(my_string, 'a'), or 
my_string.indexOf('a'). It works nice, and I use it frequently so no 
need to break that one. But it is kind of sad that it does not work for 
int as well.

bool isPrime(int this);

Jupp, what if naming the first argument this would imply that it is a 
"method" not a function? Seams quite natural to me.

Could work well with a revisited properties syntax as well. Today
T name() { ... } // and
T name(T foo) { ... }
makes up a property pair. As I said in the other thread going for:
property T name() { ... } // and
property T name(T foo) { ... }
could be "compatible enought", to allow for the old way to stay but 
depricated for while.

As a bonus:
property T name(U this) { ... } // and
property T name(U this, T foo) { ... }
Would naturaly add properties to existing types.

Well, that is all fine for unindexed properties anyway. Having used both 
Object Pascal and Visual Basic, I like their simple way of doing fake 
array properties with little extra code. As per VB:
Property Get Foo(index As Integer) As String
...
End Property

Is quite elegant and works well, both for writing and using the 
implementation (From the view of writing a compiler handling it, I have 
no clue). How to make it most elegant in D, I am not so sure, requiring 
a class with overridden opIndex and opIndexAssign, feels like an 
overkoll though.
property get T name(int i); // and
property set T name(T foo, int i);
Is the best I can do.
property T name(int i) {
   get { ... }
   set { ... }
}

pleasing to my eye, but feels far from the current way it is done.
Jan 06 2006