Scope Containers

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Mar 11 19:23:19 UTC 2019


On Mon, Mar 11, 2019 at 06:29:23PM +0000, Atila Neves via Digitalmars-d wrote:
> On Monday, 11 March 2019 at 17:00:35 UTC, bitwise wrote:
[...]
> > I do think the idea of making the container itself the range is
> > interesting, but what ruins it for me is the fact that ranges are
> > consumable.
> 
> Which is why Vector has both opSlice and copies by default. It's only
> a range itself if the element type is mutable, otherwise it doesn't
> even define popFront.
[...]

Generally, it's a bad idea to conflate a container with a range over the
container, precisely for this reason. In this respect, built-in arrays
are a bad example, because we think of them as containers, yet they are
at the same time ranges. (Though strictly speaking, "arrays" are just
slices of the underlying containers that are managed by druntime and not
really directly manipulatable. But nobody thinks of them in this way;
the concept of "array" is much easier to grasp and reason with than
"slice over druntime-managed container".)

The recommended approach is to have the container be distinct from a
range over its elements, and provide such a range via a member function,
commonly chosen to be opSlice.


T

-- 
Real men don't take backups. They put their source on a public FTP-server and let the world mirror it. -- Linus Torvalds


More information about the Digitalmars-d mailing list