www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sort!("...") with template function?

reply Vladimirs Nordholm <v vladde.net> writes:
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
Feb 25
next sibling parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
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
Feb 25
next sibling parent Vladimirs Nordholm <v vladde.net> writes:
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!
Feb 25
prev sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Monday, February 25, 2019 5:47:47 AM MST Simen Kjrs 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
Feb 25
parent reply Simen =?UTF-8?B?S2rDpnLDpXM=?= <simen.kjaras gmail.com> writes:
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
Feb 26
parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Tuesday, February 26, 2019 3:06:04 AM MST Simen Kjrs 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 Kjrs 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
Feb 26
prev sibling parent reply Andrea Fontana <nospam example.org> writes:
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
Feb 25
parent Vladimirs Nordholm <v vladde.net> writes:
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 :)
Feb 25