Bug 8326 – std.string.format results in run-time exception
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-06-30T11:19:00Z
Last change time
2013-01-02T10:13:06Z
Assigned to
nobody
Creator
puneet
Comments
Comment #0 by puneet — 2012-06-30T11:19:22Z
std.string.format throws runtime exception for BigInt and for BitArray types even though writefln works fine. The run-time error says
std.format.FormatException@std/format.d(4744): Can't convert std.bigint.BigInt to string: "string toString()" not defined
Here is a small test case:
void main()
{
import std.stdio;
import std.string;
import std.bigint;
import std.bitmanip;
BigInt aa = 100;
BitArray bb;
bb.init([true, false]);
writefln("%x", aa);
writefln("%x", bb);
writeln(format("%x", aa)); // throws exception
writeln(format("%x", bb)); // throws exception
}
Comment #1 by hsteoh — 2012-10-27T12:14:24Z
Yeah std.string.format is being deprecated, because it is an inferior version of std.format. Until it is replaced by the latter, using xformat should work (xformat simply calls std.format so it will be identical to writeln & friends).
Comment #2 by hsteoh — 2012-10-27T12:15:25Z
I meant to paste the modified code:
void main()
{
import std.stdio;
import std.string;
import std.bigint;
import std.bitmanip;
BigInt aa = 100;
BitArray bb;
bb.init([true, false]);
writefln("%x", aa);
writefln("%x", bb);
writeln(xformat("%x", aa)); // this works
writeln(xformat("%x", bb)); // this works
}