Why tuples are not ranges?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Jun 29 06:10:16 UTC 2018


On Friday, June 29, 2018 05:52:03 Alex via Digitalmars-d-learn wrote:
> On Thursday, 28 June 2018 at 19:02:51 UTC, Ali Çehreli wrote:
> > On 06/28/2018 11:08 AM, Mr.Bingo wrote:
> > > Thanks, why not add the ability to pass through ranges and
> >
> > arrays and
> >
> > > add it to phobos?
> >
> > Makes sense. It needs an enhancement request at
> > http://issues.dlang.org/, a good implementation, and a pull
> > request. :)
> >
> > Ali
>
> Wouldn't this be weird from the semantic view?
>
> Assume, I define a tuple with named fields "key" and "value",
> where a popFront is defined.
> Now, I use popFront.
> Is the "key" field still available? Why? Or, why not?
> What happens, if I name a field of a tuple "front"? Is it
> available after using popFront? And where does it point to?
>
> I mean, I use something similar in my own project, but doesn't
> one has to make a clear distinction between a container and a
> slice of this container? Or should static arrays be unified in
> the same manner?

It wouldn't make any sense to turn a Tuple into a range. However, if all of
the values are of the same type, it might make sense to create a range from
each of the values in the Tuple. Just like static arrays, they're indexable,
so there's a clear order to them. So, I suppose that Tuple could be made
sliceable, or a helper function could be created that takes a Tuple and
returns a range which wraps it.

However, Tuples frequently have different types for each of their members
(in which case, creating a range from a Tuple makes no sense at all), and
really, a Tuple is essentially a way to declare a POD struct of values
without explicitly declaring a struct. And at that point, talking about
getting a range over a Tuple is basically the same thing as talking about
creating a range from an arbitrary struct whose members all happen to have
the same type - and that would be pretty weird.

So, under limited circumstances, a range could be constructed from a Tuple,
but in general, it just strikes me as a bizarre thing to do. Tuples and
ranges are very distinct concepts, and Tuples aren't really containers.

- Jonathan M Davis




More information about the Digitalmars-d-learn mailing list