Bug 10571 – formattedWrite error with delegate and string
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-07-08T04:53:00Z
Last change time
2013-11-22T18:21:37Z
Keywords
rejects-valid
Assigned to
nobody
Creator
yebblies
Comments
Comment #0 by yebblies — 2013-07-08T04:53:05Z
I can't format a string into a delegate taking a 'const char[]'. This seems wrong to me.
----------------------------------
import std.format;
void main()
{
string buf;
formattedWrite((in char[] s) { buf ~= s; }, "%s", "hello");
assert(buf == "hello");
}
----------------------------------
DMD v2.064 DEBUG
F:\documents\desktop\d\sourcecode\phobos\std\range.d(611): Error: static assert "Cannot put a dchar into a void delegate(const(char[])) nothrow @safe"
F:\documents\desktop\d\sourcecode\phobos\std\format.d(1752): instantiated from here: put!(void delegate(const(char[])) nothrow @safe, dchar)
F:\documents\desktop\d\sourcecode\phobos\std\format.d(2146): instantiated from here: formatValue!(void delegate(const(char[])) nothrow @safe, dchar, char)
F:\documents\desktop\d\sourcecode\phobos\std\format.d(1790): instantiated from here: formatRange!(void delegate(const(char[])) nothrow @safe, string, char)
F:\documents\desktop\d\sourcecode\phobos\std\format.d(2996): ... (1 instantiations, -v to show) ...
F:\documents\desktop\d\sourcecode\phobos\std\format.d(420): instantiatedfrom here: formatGeneric!(void delegate(const(char[])) nothrow @safe, string, char)
testx.d(7): instantiated from here: formattedWrite!(void delegate(const(char[]) s) nothrow @safe, char, string)
Comment #1 by yebblies — 2013-07-27T22:33:23Z
*** This issue has been marked as a duplicate of issue 9823 ***
Comment #2 by yebblies — 2013-07-27T22:41:08Z
Ok, maybe not.
Comment #3 by monarchdodra — 2013-08-29T10:27:33Z
(In reply to comment #0)
> I can't format a string into a delegate taking a 'const char[]'. This seems
> wrong to me.
>
> ----------------------------------
>
> import std.format;
>
> void main()
> {
> string buf;
> formattedWrite((in char[] s) { buf ~= s; }, "%s", "hello");
> assert(buf == "hello");
> }
>
> ----------------------------------
Yup. The branches char/string don't work with a delegate sink that accepts a const(char)[]. Here is a somewhat reduced case.
//----
import std.format;
void main()
{
FormatSpec!char f;
formatValue((const(char)[]){}, '本', f);
formatValue((const(char)[]){}, "a", f);
}
//----
The root issue is that "formatValue" for wide characters doesn't actually work. It just calls "put" and hopes put will magically do the work :D
formatValue for strings then also fails, because it has to compile the "%r" path (raw), which iterates over the string as a range (dchars), and then prints these raw with formatValue(dchar), which, again, doesn't work.
The reason we haven't seen this failure in the testers yet, is that std.format is mostly unittest using Appender, and Appender *knows* how to transcode, making it a very poor choice for thorough testing.
In any case, this gets fixed by my "put" fix:
https://github.com/D-Programming-Language/phobos/pull/1439
Comment #4 by github-bugzilla — 2013-11-22T17:36:44Z