Forward ranges in Phobos v2

Paul Backus snarwin at gmail.com
Thu Nov 4 04:43:41 UTC 2021


On Thursday, 4 November 2021 at 04:06:15 UTC, Andrei Alexandrescu 
wrote:
> On 11/3/21 11:25 PM, Paul Backus wrote:
>> 
>> If we want to avoid copying, we can have `next` return a 
>> `Ref!T` in the case where the forward range has lvalue 
>> elements:
>> 
>> struct Ref(T)
>> {
>>      T* ptr;
>> 
>>      ref inout(T) deref() inout
>>      {
>>          return *ptr;
>>      }
>>      alias deref this;
>> }
>> 
>> I've tested some simple uses of this wrapper on run.dlang.io, 
>> and it seems like DIP 1000 is good enough to make it work in 
>> @safe code.
>> 
>> If "returns either `T` or `Ref!T`" sounds like a suspect 
>> design for an API, consider that it is basically the same 
>> thing as an `auto ref` return value--just with the distinction 
>> between ref and non-ref brought inside the type system.
>
> That was on the table, too, in the form of a raw pointer.
>
> I think it can be made to work, but for lvalue ranges only, and 
> it will be difficult to make safe (scoped etc).
>
> Overall this seems to create more problems than it solves.

I'd be curious to see any examples of such problems you have in 
mind.

As far as I'm aware, no special effort should be required to make 
this @safe, aside from enabling -preview=dip1000 (which, granted, 
is still a work in progress).


More information about the Digitalmars-d mailing list