Jump to page: 1 2
Thread overview
Dynamic arrays
Aug 31, 2015
Minas Mina
Aug 31, 2015
anonymous
Aug 31, 2015
John Colvin
Aug 31, 2015
anonymous
Sep 01, 2015
Jonathan M Davis
Aug 31, 2015
qznc
Sep 01, 2015
Minas Mina
Aug 31, 2015
jmh530
Sep 01, 2015
qznc
Sep 01, 2015
Ali Çehreli
August 31, 2015
I have started a series of tutorials in D.

This is my latest blog post, which is about dynamic arrays:
http://minas-mina.com/2015/08/31/dynamic-arrays/

Constructive criticism is welcome.
August 31, 2015
On Monday 31 August 2015 23:09, Minas Mina wrote:

> I have started a series of tutorials in D.
> 
> This is my latest blog post, which is about dynamic arrays: http://minas-mina.com/2015/08/31/dynamic-arrays/
> 
> Constructive criticism is welcome.

"Dynamic arrays are allocated on the garbage collected heap" "Dynamic arrays can be sliced"

These are not wrong, but maybe a little bit misleading.

Static arrays and pointers can be sliced, too. And a slice of a static array or pointer is ... dramatic pause ... a dynamic array that doesn't necessarily refer to the GC heap.
August 31, 2015
On Monday, 31 August 2015 at 21:29:09 UTC, anonymous wrote:
> On Monday 31 August 2015 23:09, Minas Mina wrote:
>
>> I have started a series of tutorials in D.
>> 
>> This is my latest blog post, which is about dynamic arrays: http://minas-mina.com/2015/08/31/dynamic-arrays/
>> 
>> Constructive criticism is welcome.
>
> "Dynamic arrays are allocated on the garbage collected heap" "Dynamic arrays can be sliced"
>
> These are not wrong, but maybe a little bit misleading.
>
> Static arrays and pointers can be sliced, too. And a slice of a static array or pointer is ... dramatic pause ... a dynamic array that doesn't necessarily refer to the GC heap.

I prefer the term "slice" to dynamic array. Because it's an unfamiliar term it helps prevent confusion for people who are used to what other languages call dynamic arrays.
August 31, 2015
On Monday, 31 August 2015 at 21:09:02 UTC, Minas Mina wrote:
> I have started a series of tutorials in D.
>
> This is my latest blog post, which is about dynamic arrays:
> http://minas-mina.com/2015/08/31/dynamic-arrays/
>
> Constructive criticism is welcome.

Minor error with strings. It is "immutable(char)[]" instead of "immutable(char[])".

Since this tutorial builds on your static array tutorial, you could take a quick detour for "capacity". Actually, ptr is not of type *T, but a static array of Ts. The capacity of the dynamic array is the length of the static array it contains. On the other hand, it might be too confusing...

Another thing is appending like a ~= 'x'. The tricky thing is that slices behave different than dynamic arrays here. Hm, also seems to advanced for such a basic tutorial.
August 31, 2015
On 8/31/15 5:09 PM, Minas Mina wrote:
> I have started a series of tutorials in D.
>
> This is my latest blog post, which is about dynamic arrays:
> http://minas-mina.com/2015/08/31/dynamic-arrays/
>
> Constructive criticism is welcome.

Technical difference, in an array layout, length comes first, then pointer.

shrinked -> shrunk.

string is an alias for immutable(char)[], not immutable(char[]). The parentheses placement is crucial (one can be reassigned, one cannot).

Nice article!

-Steve
August 31, 2015
On 8/31/15 5:36 PM, John Colvin wrote:
> On Monday, 31 August 2015 at 21:29:09 UTC, anonymous wrote:
>> On Monday 31 August 2015 23:09, Minas Mina wrote:
>>
>>> I have started a series of tutorials in D.
>>>
>>> This is my latest blog post, which is about dynamic arrays:
>>> http://minas-mina.com/2015/08/31/dynamic-arrays/
>>>
>>> Constructive criticism is welcome.
>>
>> "Dynamic arrays are allocated on the garbage collected heap" "Dynamic
>> arrays can be sliced"
>>
>> These are not wrong, but maybe a little bit misleading.
>>
>> Static arrays and pointers can be sliced, too. And a slice of a static
>> array or pointer is ... dramatic pause ... a dynamic array that
>> doesn't necessarily refer to the GC heap.
>
> I prefer the term "slice" to dynamic array. Because it's an unfamiliar
> term it helps prevent confusion for people who are used to what other
> languages call dynamic arrays.

I agree, but the spec calls them dynamic arrays as well. The model of the array runtime that I convey in the d array article (http://dlang.org/d-array-article.html) makes things easier to understand (at least for me), but it was kind of shunned as improper terminology :)

-Steve
August 31, 2015
On Monday, 31 August 2015 at 21:09:02 UTC, Minas Mina wrote:
> Constructive criticism is welcome.

In the "define a dynamic array" section, I often find myself writing
int[] numbers;
numbers.length = 3;
Instead of
int[] numbers = new int[3];
I understand that this fits with the resizing section later a bit better, but it's just how I typically do it.

You might show some examples of functions where you can pass dynamic arrays, but not static arrays.

In general, I find myself very easily getting mixed up by the syntax of the static vs. dynamic arrays. For instance, compare
int[] x = new int[3];
int[3] y = new int[3];
auto z = new int[3];
x and y are obviously dynamic and static arrays, respectively. However, my initial guess on z is that is like y, a static array on the heap. But that is wrong. It's actually like x. Easy to get confused.
August 31, 2015
On Monday 31 August 2015 23:36, John Colvin wrote:

> I prefer the term "slice" to dynamic array. Because it's an unfamiliar term it helps prevent confusion for people who are used to what other languages call dynamic arrays.

I'm not a fan of the term "slice". Not because I dislike the word itself, but simply because the spec uses "dynamic array" for T[].

The "D Slices" article [1] on dlang.org uses "slice" for T[], and "dynamic array" for the underlying chunk of memory. It's no surprise that people get confused when the different pages of the site can't agree on which word to use for what.

[1] http://dlang.org/d-array-article.html
September 01, 2015
On Monday, 31 August 2015 at 21:45:26 UTC, Steven Schveighoffer wrote:
> On 8/31/15 5:09 PM, Minas Mina wrote:
>> I have started a series of tutorials in D.
>>
>> This is my latest blog post, which is about dynamic arrays:
>> http://minas-mina.com/2015/08/31/dynamic-arrays/
>>
>> Constructive criticism is welcome.
>
> Technical difference, in an array layout, length comes first, then pointer.
>
> shrinked -> shrunk.
>
> string is an alias for immutable(char)[], not immutable(char[]). The parentheses placement is crucial (one can be reassigned, one cannot).
>
> Nice article!
>
> -Steve

Thanks, I have corrected the mistakes.

My intention was to keep the article shorter, but I had to write a lot of things so it grew up quickly. I decided not to talk about slicing static arrays to avoid confusion.

Thank you all for your suggestions!
September 01, 2015
On Monday, 31 August 2015 at 22:03:07 UTC, jmh530 wrote:
> In general, I find myself very easily getting mixed up by the syntax of the static vs. dynamic arrays. For instance, compare
> int[] x = new int[3];
> int[3] y = new int[3];
> auto z = new int[3];
> x and y are obviously dynamic and static arrays, respectively. However, my initial guess on z is that is like y, a static array on the heap. But that is wrong. It's actually like x. Easy to get confused.

In general, "new" means heap allocation.

I wonder about the initialization of y. Maybe it creates a temporary dynamic array on the heap and then copies it to the static array on the stack?
« First   ‹ Prev
1 2