Can't recreate a range?

Simen Kjærås simen.kjaras at gmail.com
Thu Apr 30 13:51:14 UTC 2020


On Thursday, 30 April 2020 at 13:23:25 UTC, Paul Backus wrote:
> On Thursday, 30 April 2020 at 13:04:47 UTC, Casey wrote:
>> Here's a minimal code example that duplicates the issue:
>>
>> import std.array, std.range, std.stdio, std.traits, std.string;
>>
>> auto readStream(Range)(auto ref Range r) if 
>> (isInputRange!(Unqual!Range))
>> {
>> 	struct StreamRange(Range)
>> 	{
>> 		alias R = Unqual!Range;
>> 		R _input;
>>
>> 		auto buff = appender!string;
>
> Using a default value like this means that it will be shared 
> among all instances of the struct. Instead, you should set 
> `buff = appender!string` in the constructor, so that each 
> struct will have its own appender.

Yup, that's the one. No need to assign it at all, in fact - the 
line can be replaced with

     Appender!string buff;

And things just work.

--
   Simen


More information about the Digitalmars-d-learn mailing list