D versus Objective C Comparison
Chris R Miller
lordsauronthegreat at gmail.com
Wed Feb 4 10:41:34 PST 2009
Sean Kelly wrote:
> Chris R Miller wrote:
>>
>> ===== Short Story =====
>>
>> I needed to search through a String (NSString) specifically to know
>> whether a character at a specific index is any one of a given set of
>> characters. Rather than subclass NSString, I decided to make a category:
> > [snip]
>
> Won't all of this be solved by the planned D2 feature of making these
> operations synonymous for all types?
>
> void fn(T val);
> T t;
> fn(t);
> t.fn();
>
> Or are you saying that these added functions can actually access private
> data in the class?
Yes, they can access private data. I see how this could be both a
feature and a potential hazard. Then again, do you not need to be
careful when subclassing things in Java-style OO languages? Otherwise
you can very easily mire yourself in a world of objects that end up
hindering more than helping.
And just to prove D's potency, the very algorithm I used was stolen from
something I wrote in D not long ago:
====== DuffsDevice.d ======
module duffsdevice;
import tango.io.Stdout;
/// tells if anything in the contents of arr1 are present in arr2
/// uses type implicit opCmp for comparison
bool hasAnyOf(T)(T[] arr1,T[] arr2){
T[] small,large;
if(arr1.length>arr2.length){
large=arr1;small=arr2;
}else{
large=arr2;small=arr1;
}
int lp,rm=large.length%10;
foreach(T t;small){
lp=(large.length-1)/10;
switch(rm){
case 0: do{
if(t==large[lp*10+9]) return true;
case 9: if(t==large[lp*10+8]) return true;
case 8: if(t==large[lp*10+7]) return true;
case 7: if(t==large[lp*10+6]) return true;
case 6: if(t==large[lp*10+5]) return true;
case 5: if(t==large[lp*10+4]) return true;
case 4: if(t==large[lp*10+3]) return true;
case 3: if(t==large[lp*10+2]) return true;
case 2: if(t==large[lp*10+1]) return true;
case 1: if(t==large[lp*10 ]) return true;
}while(0<--lp);
break;
}
}
return false;
}
void main(char[][] argc) {
if(argc.length>2) {
Stdout("argc[0]=={}",argc[1]).newline;
Stdout("argc[1]=={}",argc[2]).newline;
Stdout("hasAnyOf!(char[])(argc[0],argc[1])=={}",
hasAnyOf!(char)(argc[1],argc[2])).newline;
}
}
====== EOF ======
Personally I prefer the D to the Objective-C implementation.
More information about the Digitalmars-d
mailing list