Skip to content

Commit f5e5151

Browse files
authored
Merge pull request #3418 from ntrel/new-forms
[spec] Document NewExpression forms Signed-off-by: Dennis <dkorpel@users.noreply.github.com> Merged-on-behalf-of: Dennis <dkorpel@users.noreply.github.com>
2 parents 6cf7141 + 5f90415 commit f5e5151

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

spec/expression.dd

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,9 +2371,44 @@ $(GNAME ArgumentList):
23712371
collected) heap.
23722372
)
23732373

2374-
$(P If a $(I NewExpression) is used as an initializer for
2374+
$(P The *Type* form constructs an instance of a type and default-initializes it.)
2375+
$(P The *Type(ArgumentList)* form allows passing either a single initializer
2376+
of the same type, or multiple arguments for more complex types.
2377+
For class types, *ArgumentList* is passed to the class constructor.
2378+
For a dynamic array, the argument sets the initial array length.
2379+
For multidimensional dynamic arrays, each argument corresponds to
2380+
an initial length.)
2381+
2382+
$(SPEC_RUNNABLE_EXAMPLE_RUN
2383+
---
2384+
int* i = new int;
2385+
assert(*i == 0);
2386+
i = new int(5);
2387+
assert(*i == 5);
2388+
2389+
Object o = new Object;
2390+
Exception e = new Exception("info");
2391+
2392+
auto a = new int[](2);
2393+
assert(a.length == 2);
2394+
2395+
int[][] m = new int[][](10, 5);
2396+
assert(m.length == 10);
2397+
assert(m[0].length == 5);
2398+
---
2399+
)
2400+
2401+
$(P The *Type[AssignExpression]* form allocates a dynamic array with
2402+
length equal to *AssignExpression*.
2403+
It is preferred to use the *Type(ArgumentList)* form when allocating
2404+
dynamic arrays instead, as it is more general.)
2405+
2406+
$(NOTE It is not possible to allocate a static array directly with
2407+
`new` (only by using a type alias).)
2408+
2409+
$(P If a $(I NewExpression) is used with a class type as an initializer for
23752410
a function local variable with $(DDSUBLINK spec/attribute, scope, `scope`) storage class,
2376-
then the instance is allocated on the stack.
2411+
then the instance is $(DDSUBLINK spec/attribute, scope-class-var, allocated on the stack).
23772412
)
23782413

23792414
$(P `new` can also be used to allocate a

0 commit comments

Comments
 (0)