sort using delegate as predicate

via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 16 09:38:04 PDT 2014


On Tuesday, 16 September 2014 at 16:27:46 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
> On Tue, Sep 16, 2014 at 04:19:10PM +0000, via 
> Digitalmars-d-learn wrote:
>> The following code does not compile, because the custom 
>> predicate of
>> std.algorithm.sort is a template parameter, and therefore can 
>> only be
>> a function, but not a delegate.
>
> What makes you think so? Template parameters certainly can be 
> delegates.
> I use that all the time.

Well because the delegate contains a pointer to the object, which 
can not be known at compile time. In fact

> sort!(&f.cmp)(data);

fails with the error "variable f cannot be read at compile time", 
which seems reasonable to me. On the other hand

> sort!((a,b) => f.cmp(a, b))(data);

does in fact compile, so i guess problem is solved. Thanks guys.


More information about the Digitalmars-d-learn mailing list