Slices and Dynamic Arrays

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jan 1 05:18:05 UTC 2018


On Monday, January 01, 2018 05:06:46 Tony via Digitalmars-d-learn wrote:
> On Monday, 1 January 2018 at 02:10:14 UTC, Jonathan M Davis wrote:
> > The DLang Tour should probably be fixed to use the term dynamic
> > array though.
>
> Or embrace both terms but take care that it is clear that they
> are synonyms and one may be preferred depending on context. As a
> beginner, I had some confusion seeing both terms used.
>
> There is dual terminology in use outside of dlang.org. The book
> Programming In D says:
> --------------------------------------------------------------------
> Slice: Another name for dynamic array.
>
> When I write slice I will specifically mean a slice; and when I
> write array, I will mean either a slice or a fixed-length array,
> with no distinction.
>
> Slices
>
> Slices are the same feature as dynamic arrays. They are called
> dynamic arrays for being used like arrays, and are called slices
> for providing access to portions of other arrays. They allow
> using those portions as if they are separate arrays.
> -----------------------------------------------------------------------

A big problem with the term slice though is that it means more than just
dynamic arrays - e.g. you slice a container to get a range over it, so that
range is a slice of the container even though no arrays are involved at all.
So, you really can't rely on the term slice meaning dynamic array. Whether
it does or not depends on the context. That means that the fact that a
number of folks have taken to using the term slice to mean T[] like the D
Slices article talks about tends to create confusion when the context is not
clear. IMHO, the D Slices article should be updated to use the correct
terminology, but I don't think that the author is willing to do that.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list