conver BigInt to string

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Nov 5 08:45:08 PST 2015


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.


More information about the Digitalmars-d-learn mailing list