digitalmars.D.learn - what wrong with this alias
- Qusatlegadus (6/6) Jan 07 2023 auto s = 1234.to!string.map!q{a - '0'}.sum;
- Salih Dincer (12/13) Jan 08 2023 ```d
- Krzysztof =?UTF-8?B?SmFqZcWbbmljYQ==?= (32/38) Jan 08 2023 ## Simple explanation
- Salih Dincer (16/23) Jan 08 2023 A logical solution...
- Steven Schveighoffer (5/16) Jan 09 2023 Aside from the problem with the code, that error alone deserves a bug
auto s = 1234.to!string.map!q{a - '0'}.sum; works fine. but if i do an alias alias comb = to!string.map!q{a - '0'} Error: unknown, please file report on issues.dlang.org What's wrong with this alias?
Jan 07 2023
On Sunday, 8 January 2023 at 05:42:46 UTC, Qusatlegadus wrote:What's wrong with this alias?```d import std; alias comb = map!q{a - '0'}; void main() { auto s = 2234.to!string.map!q{a - '0'}.sum; s.to!string.comb.sum.writeln; // thiS works: "2" } ``` SDB 79
Jan 08 2023
On Sunday, 8 January 2023 at 05:42:46 UTC, Qusatlegadus wrote:auto s = 1234.to!string.map!q{a - '0'}.sum; works fine. but if i do an alias alias comb = to!string.map!q{a - '0'} Error: unknown, please file report on issues.dlang.org What's wrong with this`to!string` is a function expecting 1 argument, which you're not providing in your alias. Convert your alias to a lambda expression: ```D alias comb = x => x.to!string.map!q{a - '0'} ``` `to` is a template defined like this: ```D // https://github.com/dlang/phobos/blob/master/std/conv.d template to(T) { T to(A...)(A args) if (A.length > 0) { return toImpl!T(args); } // some overloads omitted for brevity } ``` `to` needs at least 2 template arguments - the first one for the outer template is passed explicitly (what you did with `to!string`), the other ones are inferred from the arguments passed to the `to` function. Since you did not pass an argument to `to!string`, the inner template doesn't get instantiated. Basically what happens is you're trying to pass an uninstantiated template as argument to `map`. This is quite an exotic situation, so probably there isn't a dedicated error message for it or you're hitting a bug in the compiler (hence the unknown error message).
Jan 08 2023
On Sunday, 8 January 2023 at 09:45:09 UTC, Krzysztof Jajeśnica wrote:`to!string` is a function expecting 1 argument, which you're not providing in your alias. Convert your alias to a lambda expression: ```D alias comb = x => x.to!string.map!q{a - '0'} ```A logical solution... Since your goal is to manipulate numbers, it is possible to convert directly to char type: ```d import std.algorithm : map, sum; import std.conv : toChars; alias comb = (uint x) => x.toChars.map!"a - '0'"; void main() { auto s = 2234.comb.sum; assert(s.comb.sum == 2); } ``` SDB 79
Jan 08 2023
On 1/8/23 12:42 AM, Qusatlegadus wrote:auto s = 1234.to!string.map!q{a - '0'}.sum; works fine. but if i do an alias alias comb = to!string.map!q{a - '0'} Error: unknown, please file report on issues.dlang.org What's wrong with this alias?Aside from the problem with the code, that error alone deserves a bug report so... https://issues.dlang.org/show_bug.cgi?id=23615 -Steve
Jan 09 2023