digitalmars.D.learn - sort!("...") with template function?
- Vladimirs Nordholm (9/9) Feb 25 2019 Hello.
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (9/18) Feb 25 2019 foos.sort!((a,b) => a.get!Dummy < b.get!Dummy);
- Vladimirs Nordholm (2/26) Feb 25 2019 Ah, thank you for the explanation Simen!
- Jonathan M Davis (9/32) Feb 25 2019 They're actually still useful in cases where you need to compare lambdas
- Simen =?UTF-8?B?S2rDpnLDpXM=?= (11/24) Feb 26 2019 Lambdas can be compared, though:
- Jonathan M Davis (5/31) Feb 26 2019 Well, that must be a relatively recent addition. It certainly didn't use...
- Andrea Fontana (5/14) Feb 25 2019 Maybe:
- Vladimirs Nordholm (2/22) Feb 25 2019 Thank you Andrea :)
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 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/9zDfddfoos.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 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:Ah, thank you for the explanation Simen!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/9zDfddfoos.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 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: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 DavisHello. 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/9zDfddfoos.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)
Feb 25 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 viaLambdas 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 -- SimenString 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.
Feb 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:Well, that must be a relatively recent addition. It certainly didn't used to exist. - Jonathan M DavisOn Monday, February 25, 2019 5:47:47 AM MST Simen Kjærås viaLambdas 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 -- SimenString 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.
Feb 26 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/9zDfddMaybe: foos.sort!((a,b) => a.get!Dummy < b.get!Dummy); Andrea
Feb 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:Thank you Andrea :)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/9zDfddMaybe: foos.sort!((a,b) => a.get!Dummy < b.get!Dummy); Andrea
Feb 25 2019