Odd to!string call

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Dec 21 11:32:56 PST 2010


And yes i know writeln() doesn't need std.conv, writeln could be any
other function expecting a string.

On 12/21/10, Andrej Mitrovic <andrej.mitrovich at gmail.com> wrote:
> Right. Thanks, guys.
>
> I do see how this could possibly cause bugs for the uninitiated.
> Someone who is new to D might attempt to use to!string with multiple
> arguments, and end up with buggy code like this:
>
> import std.stdio;
> import std.conv;
>
> void main()
> {
>     int x = 2;
>     int y = 4;
>
>     // more code..
>
>     writeln(to!string(x, y));  // writes "2", not "2 4", and not "24"
> }
>
>
> On 12/21/10, Simen kjaeraas <simen.kjaras at gmail.com> wrote:
>> Andrej Mitrovic <none at none.none> wrote:
>>
>>> I found this by accident:
>>>
>>> import std.stdio;
>>> import std.conv;
>>>
>>> void main()
>>> {
>>>     writeln(to!string(2, 2));  // writes 10
>>>     writeln(to!string(1, 0));  // std.conv.ConvException: Radix error
>>> }
>>>
>>> I'm not sure why "std.conv.to" would even take multiple arguments.
>>> Bugzilla?
>>
>> The second parameter is (as indicated by the exception) the radix[1].
>> With a radix of 2, 2 is written 10, as radix is binary. Radix 0 makes
>> no sense, and thus gives an exception.
>>
>> It could be said though, that std.conv's documentation is confusing
>> at best, and this could be worth putting in Bugzilla.
>>
>> [1]:
>> http://en.wikipedia.org/wiki/Radix#Bases_and_positional_numeral_systems
>>
>> --
>> Simen
>>
>


More information about the Digitalmars-d-learn mailing list