Bug 9074 – Can't use range functions with Appender
Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-11-25T08:14:00Z
Last change time
2017-01-02T21:29:17Z
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2012-11-25T08:14:35Z
import std.array;
import std.range;
void main()
{
// Error: casting string to char is deprecated
// Error: static assert "Cannot put a dchar into a Appender!(string)"
Appender!string x;
x.put(repeat(" ").take(4));
// Error: casting string to dchar is deprecated
Appender!dstring y;
y.put(repeat(" ").take(4));
}
It forces us to use `replicate`, which does allocation.
Comment #1 by andrej.mitrovich — 2014-04-24T19:10:02Z
Different error now:
-----
C:\dmd-git\dmd2\windows\bin\..\..\src\phobos\std\conv.d(3873): Error: static assert "immutable(char) cannot be emplaced from a string."
C:\dmd-git\dmd2\windows\bin\..\..\src\phobos\std\array.d(2522): instantiated from here: emplaceRef!string
test.d(9): instantiated from here: put!(Take!(Repeat!string))
-----
Anyone has ideas how to fix this (or if it should be fixed?).
Comment #2 by monarchdodra — 2014-04-24T20:56:30Z
(In reply to Andrej Mitrovic from comment #1)
> Different error now:
>
> -----
> C:\dmd-git\dmd2\windows\bin\..\..\src\phobos\std\conv.d(3873): Error: static
> assert "immutable(char) cannot be emplaced from a string."
> C:\dmd-git\dmd2\windows\bin\..\..\src\phobos\std\array.d(2522):
> instantiated from here: emplaceRef!string
> test.d(9): instantiated from here: put!(Take!(Repeat!string))
> -----
>
> Anyone has ideas how to fix this (or if it should be fixed?).
Yeah, use the "actual" std.range.put:
//----
void main()
{
// Error: casting string to char is deprecated
// Error: static assert "Cannot put a dchar into a Appender!(string)"
Appender!string x;
put(x, repeat(" ", 4));
// Error: casting string to dchar is deprecated
Appender!dstring y;
put(y, repeat(" ").take(4));
}
//----
This is why you should "never" use put "UFCS-style".
Always "put(a, b)".
Now, when all that is said and done, there's still an internal error, so it needs fixing.
I don't think "Appender" should be able to handle this "natively" though. It should have given a "no matching function" error.
Comment #3 by andrej.mitrovich — 2014-04-24T20:57:43Z
Ahh good catch. One of the rare downsides of UFCS.
Comment #4 by nick — 2017-01-02T21:29:17Z
Code in comment 2 works now with v2.072.2, closing.