sort!("...") with template function?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Feb 25 15:26:33 UTC 2019


On Monday, February 25, 2019 5:47:47 AM MST Simen Kjærås via Digitalmars-d-
learn wrote:
> On Monday, 25 February 2019 at 12:37:31 UTC, Vladimirs Nordholm
>
> wrote:
> > Hello.
> >
> > I wish to sort an array by calling a template function on a
> > struct. In essence I want to do
> >
> >     foos.sort!("a.get!Dummy < b.get!Dummy");
> >
> > but I get the error message
> >
> >
> > /dlang/dmd/linux/bin64/../../src/phobos/std/functional.d-mixin-215(215):
> > Error: undefined identifier Dummy
> >
> > Is there anyway I can get `sort` to recognise my Dummy type?
> >
> > Example: https://run.dlang.io/is/9zDfdd
>
> foos.sort!((a,b) => a.get!Dummy < b.get!Dummy);
>
> String functions can't access the local scope, and thus can't see
> Dummy. Using lambdas works. (string functions were basically a
> workaround for no decent lambda syntax back in the time when
> dinosaurs roamed the earth)

They're actually still useful in cases where you need to compare lambdas
(since you can't compare actual lambdas, but you can compare strings), and
in some cases, using a string lambda with a function that accepts it is less
verbose than using a regular lambda, but in general, folks do tend to use
regular lambdas now that we have the more concise lambda syntax - especially
since string lambdas do have some annoying limitations like this.

- Jonathan M Davis






More information about the Digitalmars-d-learn mailing list