Thread overview
The D Programming Language page 161.
Nov 13, 2013
Uplink_Coder
Nov 13, 2013
Adam D. Ruppe
Nov 13, 2013
Andrej Mitrovic
November 13, 2013
Hello.

I am trying to learn D using (among other things) "The D Programming Language" book.

On page 161 it says:

snippet...

void writeln(string a0, int a1, string a2, int[new] a3) {

...snippet

I am wondering about the expression "int[new] a3"

What does that mean? I have checked, and it does not compile with the current compiler - it says: Error: basic type expected, not ] ...

It is not mentioned in the errata for the book.


Tor Einar
November 13, 2013
I guess that new should be length as it represents as an integer variable
November 13, 2013
On Wednesday, 13 November 2013 at 08:16:34 UTC, Tor Einar Tønnessen wrote:
> void writeln(string a0, int a1, string a2, int[new] a3) {

Around the time the book was written, there was a debate as to if we should make dynamic arrays and slices two different types. T[new] was the proposed syntax for a dynamic array, leaving T[] to be a slice.

The reasoning would be to separate a window into the memory block - the slice - from the memory block itself, the array. This is the case with static arrays: int[10] != int[], but not with dynamic arrays. The advantage of the separation would be to help keep track of the array's ownership - appending to a T[new] could resize the block, whereas appending to a T[] would be a whole new allocation, among other things.

However, minds were changed and T[new] never actually came to be. Instead, slice append was changed to address the ownership stomping problem, it asks the gc for capacity at the end. The old problem is solved, and it didn't break any code by introducing a new type... everyone wins, except for the few things written about T[new] that weren't updated.

This article goes into the append to slice problem and solution in more detail:
http://dlang.org/d-array-article.html


Bottom line, any time you see T[new] in the book, just replace it with T[] and it should all work. So int[] a3 ought to compile and do the same thing described.

BTW, I don't see this item in the list, but on Andrei's website, there's a list of little changes since the printing:
http://erdani.com/index.php?cID=109
November 13, 2013
On 11/13/13, Adam D. Ruppe <destructionator@gmail.com> wrote:
> On Wednesday, 13 November 2013 at 08:16:34 UTC, Tor Einar Tønnessen wrote:
>> void writeln(string a0, int a1, string a2, int[new] a3) {
>
> Around the time the book was written, there was a debate as to if we should make dynamic arrays and slices two different types. T[new] was the proposed syntax for a dynamic array, leaving T[] to be a slice.

I think it was just an oversight that it was left in the book, I don't recall reading about that syntax on any other page.