Comment #0 by andrej.mitrovich — 2020-11-03T01:47:00Z
-----
import std.algorithm;
import std.conv;
import std.range;
import std.stdio;
void main () nothrow
{
int[] arr;
auto str1 = "[" ~ arr.map!(elem => elem.to!string).join(",") ~ "]"; // ok
auto str2 = arr.to!string; // not nothrow ?
}
-----
I don't quite understand why `to!string` would fail for arrays. Does it throw exceptions on OOM conditions? But if that's the case it should throw an Error, not an Exception.
Comment #1 by andrej.mitrovich — 2022-07-07T13:19:32Z
The problem is that to!() uses FormatSpec under the hood for types it doesn't specialize on.
In particular it calls `toStr`, which itself uses this:
-----
import std.array : appender;
import std.format.spec : FormatSpec;
import std.format.write : formatValue;
auto w = appender!T();
FormatSpec!(ElementEncodingType!T) f;
formatValue(w, src, f);
return w.data;
-----
Internally FormatSpec tries to parse any format specs (like "%s") and does other types of conversions like to!int which can throw exceptions, hence it cannot be nothrow.
I think this issue can be fixed as part of the larger @nogc / nothrow plans in https://forum.dlang.org/thread/[email protected].
Comment #2 by robert.schadek — 2024-12-01T16:37:48Z