Safer casts

Janice Caron caron800 at googlemail.com
Tue May 13 00:23:08 PDT 2008


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.

If Walter were to remove the built-in sort property of arrays, then
all of a sudden you would have:

    array.sort(someStaticComperator);
    array.sort(aDelegateComperator);

which I /think/ would work, because of type-deduction. (The compiler
will match it with sort!(delegate)). If it doesn't, then you could
file a bugzilla report that type deduction fails, and eventually it
will be fixed. In short, the solution to your problem is to REMOVE the
built-in property "sort" from arrays, so that libary versions can take
over.



More information about the Digitalmars-d mailing list