Skip to content

Commit 2478883

Browse files
authored
[spec/expression] Improve NewExpression docs (#3553)
Remove 1st multi-dimensional array example, link to subheading instead and add asserts there. Add 'Class Instantiation' subheading.
1 parent 4fbd4a1 commit 2478883

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

spec/expression.dd

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,13 +2517,13 @@ $(GNAME ArgumentList):
25172517
collected) heap.
25182518
)
25192519

2520-
$(P The *Type* form constructs an instance of a type and default-initializes it.)
2520+
$(P The `new` *Type* form constructs an instance of a type and default-initializes it.)
25212521
$(P The *Type(ArgumentList)* form allows passing either a single initializer
25222522
of the same type, or multiple arguments for more complex types.
25232523
For class types, *ArgumentList* is passed to the class constructor.
25242524
For a dynamic array, the argument sets the initial array length.
25252525
For multidimensional dynamic arrays, each argument corresponds to
2526-
an initial length.)
2526+
an initial length (see $(RELATIVE_LINK2 new_multidimensional, below)).)
25272527

25282528
$(SPEC_RUNNABLE_EXAMPLE_RUN
25292529
---
@@ -2537,10 +2537,6 @@ $(GNAME ArgumentList):
25372537

25382538
auto a = new int[](2);
25392539
assert(a.length == 2);
2540-
2541-
int[][] m = new int[][](10, 5);
2542-
assert(m.length == 10);
2543-
assert(m[0].length == 5);
25442540
---
25452541
)
25462542

@@ -2552,6 +2548,15 @@ $(GNAME ArgumentList):
25522548
$(NOTE It is not possible to allocate a static array directly with
25532549
`new` (only by using a type alias).)
25542550

2551+
$(P The result is a $(DDSUBLINK const3, unique-expressions, unique expression)
2552+
which can implicitly convert to other qualifiers:)
2553+
2554+
---
2555+
immutable o = new Object;
2556+
---
2557+
2558+
$(H4 $(LNAME2 new_class, Class Instantiation))
2559+
25552560
$(P If a $(I NewExpression) is used with a class type as an initializer for
25562561
a function local variable with $(DDSUBLINK spec/attribute, scope, `scope`) storage class,
25572562
then the instance is $(DDSUBLINK spec/attribute, scope-class-var, allocated on the stack).
@@ -2579,13 +2584,18 @@ $(H4 $(LNAME2 new_multidimensional, Multidimensional Arrays))
25792584

25802585
$(P To allocate the nested arrays, multiple arguments can be used:)
25812586

2587+
$(SPEC_RUNNABLE_EXAMPLE_RUN
25822588
---------------
25832589
int[][][] bar;
2584-
...
25852590
bar = new int[][][](5, 20, 30);
2591+
2592+
assert(bar.length == 5);
2593+
assert(bar[0].length == 20);
2594+
assert(bar[0][0].length == 30);
25862595
---------------
2596+
)
25872597

2588-
The code above is equivalent to:
2598+
The assignment above is equivalent to:
25892599

25902600
----------
25912601
bar = new int[][][5];

0 commit comments

Comments
 (0)