Safer casts
Yigal Chripun
yigal100 at gmail.com
Tue May 13 00:52:08 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?
I want this alias in the library. I'd prefer a sort that received some
enum (also incorporating the stable vs non-stable enum constants) but I
can settle for this, _if_ it were present in the library. Those are the
small things that make a difference. after all D does provide a string
alias too.
>
>
>> 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);
than, either the buit-in sort needs to be removed, or enhanced (thus
removing the need for the library sort), or D needs to make it possible
to overload built-in properties.
>
> 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.
OK, that is all I really wanted in the first place. my issue was with
the syntax. if it is implemented with templates but I get to use the
above syntax than I'm a 100% satisfied customer. :)
also, I didn't mention it above but this also needs to support anonymous
delegates too:
array.sort((C a, C b) {return a.val < b.val;});
I assume this works too. correct me if this is wrong.
More information about the Digitalmars-d
mailing list