Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 05, 2015 conver BigInt to string | ||||
---|---|---|---|---|
| ||||
Hello I am trying to convert BigInt to string like that while trying to sort it: string s1 = to!string(a).dup.sort; and get an error cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string what do I do wrong? |
November 05, 2015 Re: conver BigInt to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
> Hello I am trying to convert BigInt to string like that while trying to sort it:
>
> string s1 = to!string(a).dup.sort;
>
> and get an error
>
> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>
> what do I do wrong?
try
".idup"
otherwise
"auto s1 = "
|
November 05, 2015 Re: conver BigInt to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to BBasile | On Thursday, 5 November 2015 at 16:35:01 UTC, BBasile wrote:
> On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
>> Hello I am trying to convert BigInt to string like that while trying to sort it:
>>
>> string s1 = to!string(a).dup.sort;
>>
>> and get an error
>>
>> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>>
>> what do I do wrong?
>
> try
>
> ".idup"
>
> otherwise
>
> "auto s1 = "
auto did it, but idup leads to
Error: can only sort a mutable array
|
November 05, 2015 Re: conver BigInt to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Thursday, 5 November 2015 at 16:39:03 UTC, Namal wrote:
> On Thursday, 5 November 2015 at 16:35:01 UTC, BBasile wrote:
>> On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
>>> Hello I am trying to convert BigInt to string like that while trying to sort it:
>>>
>>> string s1 = to!string(a).dup.sort;
>>>
>>> and get an error
>>>
>>> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>>>
>>> what do I do wrong?
>>
>> try
>>
>> ".idup"
>>
>> otherwise
>>
>> "auto s1 = "
>
> auto did it, but idup leads to
>
> Error: can only sort a mutable array
sorry, I feel embarrassed now...good luck i wish you.
|
November 05, 2015 Re: conver BigInt to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
> Hello I am trying to convert BigInt to string like that while trying to sort it:
>
> string s1 = to!string(a).dup.sort;
>
> and get an error
>
> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>
> what do I do wrong?
Try this instead:
string s1 = to!string(a).idup.sort()
I suspect there are two things happening here. First, calling dup on a string yields char[], not string (the difference being that string is immutable and char[] is not). A char[] cannot implicitly convert to string due to the difference in immutability, although the compiler *should* realize that the result of dup is a "unique" expression and be able to implicitly convert it... It may be because you then pass it to sort, which must keep it as a char[].
The second issue is that using .sort instead of .sort() (note the parentheses) calls the built-in sort instead of std.algorithm.sort, which is strongly discouraged. You should always use std.algorithm.sort over the built-in sort, which you can do just by appending those parentheses.
|
November 05, 2015 Re: conver BigInt to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Thursday, 5 November 2015 at 16:45:10 UTC, Meta wrote: > On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote: >> Hello I am trying to convert BigInt to string like that while trying to sort it: >> >> string s1 = to!string(a).dup.sort; >> >> and get an error >> >> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string >> >> what do I do wrong? > > Try this instead: > > string s1 = to!string(a).idup.sort() > If I try it like that i get: Error: template std.algorithm.sorting.sort cannot deduce function from argument types !()(char[]), candidates are: /../src/phobos/std/algorithm/sorting.d(996): |
November 05, 2015 Re: conver BigInt to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | On Thursday, 5 November 2015 at 16:53:50 UTC, Namal wrote:
> On Thursday, 5 November 2015 at 16:45:10 UTC, Meta wrote:
>> On Thursday, 5 November 2015 at 16:29:30 UTC, Namal wrote:
>>> Hello I am trying to convert BigInt to string like that while trying to sort it:
>>>
>>> string s1 = to!string(a).dup.sort;
>>>
>>> and get an error
>>>
>>> cannot implicitly convert expression (_adSortChar(dup(to(a)))) of type char[] to string
>>>
>>> what do I do wrong?
>>
>> Try this instead:
>>
>> string s1 = to!string(a).idup.sort()
>>
> If I try it like that i get:
>
> Error: template std.algorithm.sorting.sort cannot deduce function from argument types !()(char[]), candidates are:
>
> /../src/phobos/std/algorithm/sorting.d(996):
string s1 = to!string(a).dup.sort.idup;
|
November 05, 2015 Re: conver BigInt to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ilya Yaroshenko | On Thursday, 5 November 2015 at 17:13:07 UTC, Ilya Yaroshenko wrote:
> string s1 = to!string(a).dup.sort.idup;
well, but I was just told not to use sort ??
|
November 05, 2015 Re: conver BigInt to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namal | Namal: > Hello I am trying to convert BigInt to string like that while trying to sort it: void main() { import std.stdio, std.algorithm, std.conv, std.bigint, std.string; auto n = 17.BigInt ^^ 179; n.text.dup.representation.sort().release.assumeUTF.writeln; } Bye, bearophile |
November 05, 2015 Re: conver BigInt to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > void main() { > import std.stdio, std.algorithm, std.conv, std.bigint, std.string; > > auto n = 17.BigInt ^^ 179; > n.text.dup.representation.sort().release.assumeUTF.writeln; > } Better: n.to!(char[]).representation.sort().release.assumeUTF.writeln; Bye, bearophile |
Copyright © 1999-2021 by the D Language Foundation