www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - toChars Bug?

reply Bottled Gin <gin bottle.com> writes:
Greetings

This small code snippet works:

//
import std.conv;
import std.stdio;

void main() {
   writeln(toChars!10(45));
}


But if I change toChars!10 with toChars!2, I get:

/tmp/test.d(6): Error: template std.conv.toChars cannot deduce 
function from argument types !(2)(int), candidates are:
[snip]std/conv.d(6020):        std.conv.toChars(ubyte radix = 10, 
Char = char, LetterCase letterCase = LetterCase.lower, T)(T 
value) if ((radix == 2 || radix == 8 || radix == 10 || radix == 
16) && (is(Unqual!T == uint) || is(Unqual!T == ulong) || radix == 
10 && (is(Unqual!T == int) || is(Unqual!T == long))))

toChars!8 and toChars!16 do not work either.
Dec 12 2017
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
see documentation: http://dpldocs.info/experimental-docs/std.conv.toChars.html

"...Can be uint or ulong. If radix is 10, can also be int or long."

45 is int, not uint. so no radices except `10` will work.
Dec 12 2017
next sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Tuesday, 12 December 2017 at 12:49:32 UTC, ketmar wrote:
 see documentation: 
 http://dpldocs.info/experimental-docs/std.conv.toChars.html

 "...Can be uint or ulong. If radix is 10, can also be int or 
 long."

 45 is int, not uint. so no radices except `10` will work.
I think it would be possible to alter toChars such that it had a set of overloads, such that value range propagation would allow an implicit conversion here.
Dec 12 2017
next sibling parent ketmar <ketmar ketmar.no-ip.org> writes:
John Colvin wrote:

 On Tuesday, 12 December 2017 at 12:49:32 UTC, ketmar wrote:
 see documentation: 
 http://dpldocs.info/experimental-docs/std.conv.toChars.html

 "...Can be uint or ulong. If radix is 10, can also be int or long."

 45 is int, not uint. so no radices except `10` will work.
I think it would be possible to alter toChars such that it had a set of overloads, such that value range propagation would allow an implicit conversion here.
yeah, i think that overloads with explicit `uint` and `ulong` args should take care of that.
Dec 12 2017
prev sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
p.s.: but no, i am wrong.

	foo(-42);

this is perfectly valid for `foo (uint n)`, as D converts negative ints to 
uints without any warnings.

so no, overloads won't fit.
Dec 12 2017
parent John Colvin <john.loughran.colvin gmail.com> writes:
On Tuesday, 12 December 2017 at 15:19:48 UTC, ketmar wrote:
 p.s.: but no, i am wrong.

 	foo(-42);

 this is perfectly valid for `foo (uint n)`, as D converts 
 negative ints to uints without any warnings.

 so no, overloads won't fit.
hmm yes, it seems it is not possible.
Dec 12 2017
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 12/12/17 7:49 AM, ketmar wrote:
 see documentation: 
 http://dpldocs.info/experimental-docs/std.conv.toChars.html
 
 "...Can be uint or ulong. If radix is 10, can also be int or long."
 
 45 is int, not uint. so no radices except `10` will work.
So, the answer is: toChars!2(45u); BTW, I find this limitation is a bad code smell. IFTI needs some design thought on how to deal with literals. -Steve
Dec 12 2017