Forward ranges in Phobos v2

Paul Backus snarwin at gmail.com
Tue Nov 2 00:13:31 UTC 2021


On Tuesday, 2 November 2021 at 00:05:48 UTC, Alexandru Ermicioi 
wrote:
> On Monday, 1 November 2021 at 23:46:07 UTC, H. S. Teoh wrote:
>> Good question, ask Andrei. ;-)
>
> Well, I hope he will check this thread and comment on it.
>
>> Presumably, if we standardize on structs/classes, it could be 
>> as simple as:
>>
>> 	auto myFunc(R)(R range) if (is(R == struct)) {
>> 		... // forward range
>> 	}
>>
>> 	auto myFunc(R)(R range) if (is(R == class)) {
>> 		... // input range
>> 	}
>>
>> But given that Andrei thinks it's a mistake for ranges to be 
>> implemented as classes, I've no idea.
>
> That would work, if you have templated funcs, but what if you 
> need it in an interface?
>
> If class based ranges are to be in D language, I doubt it will 
> be possible to avoid .save function completely. At least for 
> range interfaces, the save of forward range will have to be 
> expressed through a method, such as .save.

You can always wrap a class/interface method in a struct that 
calls .save on copy:


struct ClassRangeWrapper(T)
     if (is(T == class) || is(T == interface))
{
     T payload;
     alias payload this;

     this(ref inout typeof(this) other) inout
     {
         this.payload = other.payload.save;
     }
}

This way, range algorithms don't need to know about .save, so it 
can be removed from the official forward range requirements even 
though it still exists as an implementation detail.


More information about the Digitalmars-d mailing list