Jump to page: 1 2 3
Thread overview
Slices and Dynamic Arrays
Dec 29, 2017
Tony
Dec 29, 2017
Adam D. Ruppe
Dec 29, 2017
Muld
Dec 29, 2017
Jonathan M Davis
Dec 31, 2017
Tony
Dec 31, 2017
Jonathan M Davis
Dec 31, 2017
Tony
Jan 01, 2018
Jonathan M Davis
Jan 01, 2018
Tony
Jan 01, 2018
Jonathan M Davis
Jan 02, 2018
Jonathan M Davis
Jan 02, 2018
Ali Çehreli
Jan 02, 2018
Jonathan M Davis
Jan 02, 2018
Ali Çehreli
Jan 01, 2018
Seb
Dec 31, 2017
Ivan Trombley
Dec 31, 2017
Tony
Dec 31, 2017
codephantom
Dec 31, 2017
Tony
Dec 31, 2017
Jonathan M Davis
December 29, 2017
In DLang Tour:Arrays
https://tour.dlang.org/tour/en/basics/arrays

there is:
-----------------------------------------------
int size = 8; // run-time variable
int[] arr = new int[size];

The type of arr is int[], which is a slice.
-----------------------------------------------

In "D Slices"
https://dlang.org/d-array-article.html

there is:
-----------------------------------------------
int[] a;             // a is a slice
------------------------------------------------

Based on those two web pages it appears that the name for a dynamic array <reference?> in D is "slice". That is, anytime you have a dynamic array (even a null reference version) it is called a slice. Is that correct?
December 29, 2017
On Friday, 29 December 2017 at 22:32:01 UTC, Tony wrote:
> Based on those two web pages it appears that the name for a dynamic array <reference?> in D is "slice". That is, anytime you have a dynamic array (even a null reference version) it is called a slice. Is that correct?

Not exactly, but basically. read this for a bit more info https://dlang.org/d-array-article.html


A slice is a bit more general than a dynamic array. You can slice into a static array, or any buffer really. The slice just also acts as a dynamic array and can create one on-demand if you append to it, and also slices are how D represents the underlying dynamic arrays to user code.
December 29, 2017
On Friday, 29 December 2017 at 22:32:01 UTC, Tony wrote:
> In DLang Tour:Arrays
> https://tour.dlang.org/tour/en/basics/arrays
>
> there is:
> -----------------------------------------------
> int size = 8; // run-time variable
> int[] arr = new int[size];
>
> The type of arr is int[], which is a slice.
> -----------------------------------------------
>
> In "D Slices"
> https://dlang.org/d-array-article.html
>
> there is:
> -----------------------------------------------
> int[] a;             // a is a slice
> ------------------------------------------------
>
> Based on those two web pages it appears that the name for a dynamic array <reference?> in D is "slice". That is, anytime you have a dynamic array (even a null reference version) it is called a slice. Is that correct?

Not really, cause you can take a "slice" of a linked list (though inefficiently), but a linked list isn't an array. You can also take a "slice" of a stack allocated array.

December 29, 2017
On Friday, December 29, 2017 22:32:01 Tony via Digitalmars-d-learn wrote:
> In DLang Tour:Arrays https://tour.dlang.org/tour/en/basics/arrays
>
> there is:
> -----------------------------------------------
> int size = 8; // run-time variable
> int[] arr = new int[size];
>
> The type of arr is int[], which is a slice.
> -----------------------------------------------
>
> In "D Slices"
> https://dlang.org/d-array-article.html
>
> there is:
> -----------------------------------------------
> int[] a;             // a is a slice
> ------------------------------------------------
>
> Based on those two web pages it appears that the name for a dynamic array <reference?> in D is "slice". That is, anytime you have a dynamic array (even a null reference version) it is called a slice. Is that correct?

No. The term "slice" is a bit overused in D, meaning a variety of things. It doesn't help that some folks dislike the official terminology. In general, a slice is a contiguous group of elements. A slice of memory would be a contiguous block of memory. A dynamic array therefore refers to a slice of memory and could be called a slice, but it's also the case that using the slice operater on a container is called slicing - e.g. rbt[] would give you a range over the container rbt, and that range is a slice of the container, but it's not an array at all.

The "D Slices" article is great for explaining how things work but does not use the official terminology. Per the official terminology, T[] is a dynamic array regardless of what memory it refers to. Assuming that it's non-null, it _does_ refer to a slice of memory, and a number of folks follow the article and call T[] a slice and refer to the GC-allocated memory that a T[] usually refers to as the dynamic array, but officially, T[] is the dynamic array, and the memory it refers to has no special name. It's just a block of memory that is usually GC-allocated, but it could be malloced or be a slice of a static array or any other type of memory (since you can create a dynamic array from pointers), and its semantics don't change based on what type of memory backs it.

I gather that the author chose to refer to the GC-allocated block of memory as the dynamic array because of how the term dynamic array is sometimes used elsewhere in computer science, but as far as D is concerned, T[] is a dynamic array regardless of what memory it refers to, and personally, I think that focusing on the GC-allocated memory block as being the dynamic array causes confusion when dealing with dynamic arrays that refer to non-GC-allocated memory, since the semantics are identical regardless of what memory backs the array (it's just that if the array does not refer to a slice of GC-allocated memory, the capacity is always 0, guaranteed that appending will reallocate instead of it just being a possibility), but too many folks tend to think of them as being different. A dynamic array that refers to non-GC-allocated memory is really the same thing as one that is GC-allocated except that when you slice non-GC-allocated memory to create a dynamic array, you then need to be aware of how to manage the lifetime of that memory so that no dynamic array refering to it outlives it (just like you'd have to do with a pointer to non-GC-allocated memory). But dynamic arrays never manage their own memory. It's just that the GC does the managing by default and will reallocate the memory backing a dynamic array if an operation can't be done in-place.

- Jonathan M Davis

December 31, 2017
On Friday, 29 December 2017 at 23:13:20 UTC, Jonathan M Davis wrote:
> The term "slice" is a bit overused in D, meaning a variety of things. It doesn't help that some folks dislike the official terminology. In general, a slice is a contiguous group of elements. A slice of memory would be a contiguous block of memory. A dynamic array therefore refers to a slice of memory and could be called a slice, but it's also the case that using the slice operater on a container is called slicing - e.g. rbt[] would give you a range over the container rbt, and that range is a slice of the container, but it's not an array at all.
>

For me, it is confusing to use "slice" and "dynamic array" as synonyms. My initial impression was that they must have different code underlying them, and different behavior. I would pick one or the other. It should be:

D Arrays
  - Static
  - Dynamic

or

D Arrays
   - Static
   - Slice


The DLang Tour has a section on Slices that says in bold "Slices and dynamic arrays are the same". I think that sentence deserves an explanation as to why there are two terms being utilized for the same thing. I would prefer that "slice" as a noun was used only for the time when a dynamic array was initialized from a slice of another array. Or better yet - slice was never used as a noun - only a verb or adjective: took a slice of array A to form a slice dynamic array B (or slice-intialized dynamic array B).

D Arrays
   - Static
   - Dynamic
      - Slice-Initialized Dynamic
December 31, 2017
double[] D = [3.14159];

Can you guess what D is?  :D

December 31, 2017
On Sunday, 31 December 2017 at 03:08:05 UTC, Ivan Trombley wrote:
> double[] D = [3.14159];
>
> Can you guess what D is?  :D

It took me a while but I finally came up with "a slice of pi"
December 31, 2017
On Sunday, 31 December 2017 at 03:57:17 UTC, Tony wrote:
> On Sunday, 31 December 2017 at 03:08:05 UTC, Ivan Trombley wrote:
>> double[] D = [3.14159];
>>
>> Can you guess what D is?  :D
>
> It took me a while but I finally came up with "a slice of pi"

a slice of pi is irrational.
December 31, 2017
On Sunday, 31 December 2017 at 04:20:28 UTC, codephantom wrote:
> On Sunday, 31 December 2017 at 03:57:17 UTC, Tony wrote:
>> On Sunday, 31 December 2017 at 03:08:05 UTC, Ivan Trombley wrote:
>>> double[] D = [3.14159];
>>>
>>> Can you guess what D is?  :D
>>
>> It took me a while but I finally came up with "a slice of pi"
>
> a slice of pi is irrational.

Even on special occasions?
December 31, 2017
On 12/30/17 8:57 PM, Tony wrote:
> For me, it is confusing to use "slice" and "dynamic array" as synonyms. My initial impression was that they must have different code underlying them, and different behavior.

As stated in the slices article, I think of them as separate -- the slice is the public type that is used to operate on dynamic arrays, the dynamic array is a nameless type that only exists in the runtime. It helped me immensely when rewriting the array runtime to think of it that way. All the feedback I received when publishing the article was along the lines of "Wow, thinking of it that way helps a lot", so I think it's a good mental model.

But in terms of D types, the slice *is* the dynamic array type, it behaves in all the ways you would expect a dynamic array type to behave. The only difference is that a slice does not own the memory it references. Normally you would consider a dynamic array to own its memory (i.e. be responsible for allocation and destruction). Because we have the GC, ownership can be fuzzy.

> The DLang Tour has a section on Slices that says in bold "Slices and dynamic arrays are the same". I think that sentence deserves an explanation as to why there are two terms being utilized for the same thing. I would prefer that "slice" as a noun was used only for the time when a dynamic array was initialized from a slice of another array. Or better yet - slice was never used as a noun - only a verb or adjective: took a slice of array A to form a slice dynamic array B (or slice-intialized dynamic array B).

The question really is, what is the name of the type T[]? If you give it a different name depending on how it was created, then you have all sorts of confusion. In fact, there was a proposal by Walter and Andrei a long long time ago to have a type T[new] which would be the dynamic array type, and T[] be the slice type. This failed, because in Andrei's words, "Explaining two very similar but subtly different types to newcomers is excruciatingly difficult."

IMO, T[] is a slice, because it may not be GC-backed dynamic array data underneath. Some people prefer to think of it as a dynamic array, because you can use it like a dynamic array no matter what it points at. Either way works, and fits the implementation. It's really a matter of preference.

-Steve
« First   ‹ Prev
1 2 3