Skip to content

Commit 4b82606

Browse files
ntreldlang-bot
authored andcommitted
[spec/expression] Improve NewExpression docs
Add INLINE_GRAMMAR macro, like TT but smaller. List the result type of `new`. Use a list for *Type(NamedArgumentList)* usage. Tweak example. Fix link to unique expressions.
1 parent e728b06 commit 4b82606

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

dlang.org.ddoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,13 @@ GLINK_LEX=$(DDSUBLINK spec/lex,$1,$(I $1))
184184
GLOSSARY = $(HTTP dlang.org/spec/glossary.html#$0, $0)
185185
GLOSSARY2 = $(HTTP dlang.org/spec/glossary.html#$1, $2)
186186
GNAME=<a id="$0">$(SPANC gname, $0)</a>
187+
188+
_=Any macro beginning with GRAMMAR will be picked up by ddoc/source/preprocessor.d
187189
GRAMMAR=$(TC pre, bnf notranslate, $0)
188190
INFORMATIVE_GRAMMAR=$(GRAMMAR $0)
189191
GRAMMAR_LEX=$(GRAMMAR $0)
192+
INLINE_GRAMMAR=$(TC tt, bnf notranslate, $0)
193+
190194
GREEN=$(SPANC green, $0)
191195
GSELF=$(I $0)
192196
GT=&gt;

spec/expression.dd

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,38 +2820,45 @@ $(GNAME NewExpression):
28202820
collected) heap by default.
28212821
)
28222822

2823-
$(P The `new` *Type* form constructs an instance of a type and default-initializes it.)
2824-
$(P The *Type(NamedArgumentList)* form allows passing either a single initializer
2825-
of the same type, or multiple arguments for more complex types.
2826-
For class types, *NamedArgumentList* is passed to the class constructor.
2827-
For a dynamic array, the argument sets the initial array length.
2828-
For multidimensional dynamic arrays, each argument corresponds to
2829-
an initial length (see $(RELATIVE_LINK2 new_multidimensional, below)).)
2823+
$(P `new T` constructs an instance of type `T` and default-initializes it.
2824+
The result is:)
2825+
* `T` when `T` is a reference type (e.g. class, dynamic array,
2826+
$(DDSUBLINK spec/hash-map, construction_and_ref_semantic, associative array))
2827+
* `T*` when `T` is a value type (e.g. basic types, structs)
2828+
2829+
$(P The $(INLINE_GRAMMAR *Type(NamedArgumentList)*) form allows passing either a single initializer
2830+
of the same type, or multiple arguments for more complex types:)
2831+
2832+
* For class and struct types, *NamedArgumentList* is passed to the constructor.
2833+
* For a dynamic array, the argument sets the initial array length.
2834+
* For multidimensional dynamic arrays, each argument corresponds to
2835+
an initial length (see $(RELATIVE_LINK2 new_multidimensional, below)).
28302836

28312837
$(SPEC_RUNNABLE_EXAMPLE_RUN
28322838
---
28332839
int* i = new int;
2834-
assert(*i == 0);
2840+
assert(*i == 0); // int.init
28352841
i = new int(5);
28362842
assert(*i == 5);
28372843

28382844
Object o = new Object;
28392845
Exception e = new Exception("info");
28402846

2841-
auto a = new int[](2);
2847+
int[] a = new int[](2);
28422848
assert(a.length == 2);
2849+
a = new int[2]; // same, see below
28432850
---
28442851
)
28452852

2846-
$(P The *Type[AssignExpression]* form allocates a dynamic array with
2853+
$(P The $(INLINE_GRAMMAR *Type[AssignExpression]*) form allocates a dynamic array with
28472854
length equal to *AssignExpression*.
2848-
It is preferred to use the *Type(NamedArgumentList)* form when allocating
2855+
It is preferred to use the $(INLINE_GRAMMAR *Type(NamedArgumentList)*) form when allocating
28492856
dynamic arrays instead, as it is more general.)
28502857

28512858
$(NOTE It is not possible to allocate a static array directly with
28522859
`new` (only by using a type alias).)
28532860

2854-
$(P The result is a $(DDSUBLINK const3, unique-expressions, unique expression)
2861+
$(P The result is a $(DDSUBLINK spec/const3, unique-expressions, unique expression)
28552862
which can implicitly convert to other qualifiers:)
28562863

28572864
---

0 commit comments

Comments
 (0)