www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - what wrong with this alias

reply Qusatlegadus <no thanks.com> writes:
     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
next sibling parent Salih Dincer <salihdb hotmail.com> writes:
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
prev sibling next sibling parent reply Krzysztof =?UTF-8?B?SmFqZcWbbmljYQ==?= <krzysztof.jajesnica gmail.com> writes:
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
parent Salih Dincer <salihdb hotmail.com> writes:
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
prev sibling parent Steven Schveighoffer <schveiguy gmail.com> writes:
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