Safer casts
Sean Kelly
sean at invisibleduck.org
Tue May 13 07:56:39 PDT 2008
Janice Caron wrote:
> 2008/5/13 Yigal Chripun <yigal100 at gmail.com>:
>> I want the following API:
>> (I don't care now for the internal implementation and the wrapper
>> functions/aliases should be provided by the standard library)
>>
>> array.sort; //same as array.sort(ASC);
>
> That one already works.
>
>> array.sort(DESC);
>
> alias std.algorithm.sort!("b<a") sort_reverse;
> array.sort_reverse;
>
> Is that close enough?
>
>
>> array.sort(someStaticComperator);
>> array.sort(aDelegateComperator);
>> array.someOtherfunction(params);
>
> That will never be possible either with or without templates.
>
> Guess why?
>
> Well, to save you guessing, I'll tell you. It's because "sort" is a
> built-in property of D's dynamic arrays. (Like length and ptr are
> built in). In general, we can define a function foo upon arrays which
> can be called as either
>
> foo(array,otherParams);
> array.foo(otherParams);
>
> But unfortunately, you can't do that if the function happens to be
> named "sort". (Likewise "length", "ptr", "init", and so on). As I
> said, this is /not/ a limitation of templates.
Wow, then tango.core.Array.sort must work by magic! I had no idea Tango
had such powers:
C:\code\src\d\test>cat test.d
import tango.core.Array;
void main()
{
int[] buf = ([1,2,3]).dup;
buf.sort( (int a, int b){ return a < b; } );
}
C:\code\src\d\test>dmd test
C:\code\src\d\test>
Sean
More information about the Digitalmars-d
mailing list