Range Redesign: Copy Semantics

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jan 22 02:10:32 UTC 2024


On Sunday, January 21, 2024 7:51:33 AM MST Paul Backus via Digitalmars-d 
wrote:
> On Sunday, 21 January 2024 at 05:00:31 UTC, Jonathan M Davis
>
> wrote:
> > From what I can see, the main negative is simply that you can't
> > then write code that works on both a basic input range and a
> > forward range (though you can obviously still create function
> > overloads so that the caller can use either)
>
> With the proposed design, it would be possible to implement
> next() for forward ranges as a UFCS function:
>
>      auto next(FR)(FR fr)
>          if (std.v2.range.isForwardRange!FR)
>      {
>          if (fr.empty)
>          {
>              return Nullable!(ElementType!FR).init;
>          }
>          else
>          {
>              scope(success) fr.popFront;
>              return nullable(fr.front);
>          }
>      }
>
> So, a function written to operate on basic input ranges would be
> able to accept any kind of range, same as today.

True, but then you have a function with the same copy semantics problem that
we have now, since the forward range wouldn't have reference semantics. It
would need to be fully wrapped in another type which gave it reference
semantics to do that.

So, while someone could do something like this, I question that we should
encourage it or support it with anything in Phobos.

- Jonathan M Davis





More information about the Digitalmars-d mailing list