Thread overview
sort!("...") with template function?
Feb 25, 2019
Vladimirs Nordholm
Feb 25, 2019
Simen Kjærås
Feb 25, 2019
Vladimirs Nordholm
Feb 25, 2019
Jonathan M Davis
Feb 26, 2019
Simen Kjærås
Feb 26, 2019
Jonathan M Davis
Feb 25, 2019
Andrea Fontana
Feb 25, 2019
Vladimirs Nordholm
February 25, 2019
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
February 25, 2019
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)

--
  Simen
February 25, 2019
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

Maybe:
foos.sort!((a,b) => a.get!Dummy < b.get!Dummy);

Andrea
February 25, 2019
On Monday, 25 February 2019 at 12:47:47 UTC, Simen Kjærås 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)
>
> --
>   Simen

Ah, thank you for the explanation Simen!
February 25, 2019
On Monday, 25 February 2019 at 12:47:50 UTC, Andrea Fontana 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
>
> Maybe:
> foos.sort!((a,b) => a.get!Dummy < b.get!Dummy);
>
> Andrea

Thank you Andrea :)

February 25, 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




February 26, 2019
On Monday, 25 February 2019 at 15:26:33 UTC, Jonathan M Davis wrote:
> On Monday, February 25, 2019 5:47:47 AM MST Simen Kjærås via
>> 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.

Lambdas can be compared, though:

    static assert(__traits(isSame, a => a, i => i));
    alias fn1 = a => a * 2;
    alias fn2 = b => b * 2;
    static assert(__traits(isSame, fn1, fn2));

There are limits, as specified on https://dlang.org/spec/traits.html#isSame

--
  Simen
February 26, 2019
On Tuesday, February 26, 2019 3:06:04 AM MST Simen Kjærås via Digitalmars-d- learn wrote:
> On Monday, 25 February 2019 at 15:26:33 UTC, Jonathan M Davis
>
> wrote:
> > On Monday, February 25, 2019 5:47:47 AM MST Simen Kjærås via
> >
> >> 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.
>
> Lambdas can be compared, though:
>
>      static assert(__traits(isSame, a => a, i => i));
>      alias fn1 = a => a * 2;
>      alias fn2 = b => b * 2;
>      static assert(__traits(isSame, fn1, fn2));
>
> There are limits, as specified on https://dlang.org/spec/traits.html#isSame
>
> --
>    Simen

Well, that must be a relatively recent addition. It certainly didn't used to exist.

- Jonathan M Davis