toString refactor in druntime

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 3 17:16:16 PST 2014


On 11/3/2014 2:33 PM, Steven Schveighoffer wrote:
> On 11/3/14 4:40 PM, Walter Bright wrote:
>> On 11/3/2014 8:09 AM, Steven Schveighoffer wrote:
>>> It is a huge difference to say EVERYONE who implements toString will
>>> take any
>>> templated type that purports to be an output range, vs giving one case
>>> to handle.
>>
>> All an output range is is a type with a 'put' method. That's it. You're
>> making it out to be far more complex than it is.
>>
>
> Directly from the docs: (http://dlang.org/phobos/std_range.html#isOutputRange)
>
> void myprint(in char[] s) { }
> static assert(isOutputRange!(typeof(&myprint), char));
>
> No 'put' in sight, except as a substring of isOutputRange.
>
> I don't think you realize what a beast supporting all output ranges is, or using
> them (hint: calling r.put for a generic output range is an ERROR).

The documentation says, more specifically, that the requirement is that it 
support put(r,e). The array operands are output ranges NOT because the output 
ranges need to know about arrays, but because arrays themselves have a put() 
operation (as defined in std.array).

All the algorithm (such as a toString()) needs to do is call put(r,e). It 
doesn't need to do anything else. It isn't any more complicated than the sink 
interface.


More information about the Digitalmars-d mailing list