Skip to content

Commit a772e97

Browse files
authored
[spec/expression] Tweak new docs (#3909)
Split example in 2. Show that allocating an array requires a length argument. Add asserts. Add link to NewExpression from array page.
1 parent bcf4953 commit a772e97

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

spec/arrays.dd

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,17 +1011,24 @@ $(H3 $(LNAME2 default-initialization, Default Initialization))
10111011
)
10121012

10131013
$(H3 $(LNAME2 length-initialization, Length Initialization))
1014+
10141015
$(P The $(D new) expression can be used to allocate a dynamic array
10151016
with a specified length by specifying its type and then using the
10161017
`(size)` syntax:
10171018
)
10181019

1019-
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
1020+
$(SPEC_RUNNABLE_EXAMPLE_RUN
10201021
---------
1021-
int[] i = new int[](5); //i.length == 5
1022-
int[][] j = new int[][](10, 5); //j.length == 10, j[0].length == 5
1022+
int[] i = new int[](5);
1023+
i = new int[5]; // same allocation, alternate syntax
1024+
assert(i.length == 5);
1025+
1026+
int[][] j = new int[][](10, 5);
1027+
assert(j.length == 10);
1028+
assert(j[0].length == 5);
10231029
---------
10241030
)
1031+
$(P See $(GLINK2 expression, NewExpression) for details.)
10251032

10261033
$(H3 $(LNAME2 void-initialization, Void Initialization))
10271034

spec/expression.dd

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2858,11 +2858,20 @@ $(GNAME NewExpression):
28582858
)
28592859

28602860
$(P `new T` constructs an instance of type `T` and default-initializes it.
2861-
The result is:)
2862-
* `T` when `T` is a reference type (e.g. class, dynamic array,
2863-
$(DDSUBLINK spec/hash-map, construction_and_ref_semantic, associative array))
2861+
The result's type is:)
2862+
* `T` when `T` is a reference type (e.g. classes,
2863+
$(DDSUBLINK spec/hash-map, construction_and_ref_semantic, associative arrays))
28642864
* `T*` when `T` is a value type (e.g. basic types, structs)
28652865

2866+
$(SPEC_RUNNABLE_EXAMPLE_RUN
2867+
---
2868+
int* i = new int;
2869+
assert(*i == 0); // int.init
2870+
2871+
Object o = new Object;
2872+
//int[] a = new int[]; // error, need length argument
2873+
---
2874+
)
28662875
$(P The $(INLINE_GRAMMAR *Type(NamedArgumentList)*) form allows passing either a single initializer
28672876
of the same type, or multiple arguments for more complex types:)
28682877

@@ -2873,13 +2882,11 @@ $(GNAME NewExpression):
28732882

28742883
$(SPEC_RUNNABLE_EXAMPLE_RUN
28752884
---
2876-
int* i = new int;
2877-
assert(*i == 0); // int.init
2878-
i = new int(5);
2885+
int* i = new int(5);
28792886
assert(*i == 5);
28802887

2881-
Object o = new Object;
28822888
Exception e = new Exception("info");
2889+
assert(e.msg == "info");
28832890

28842891
int[] a = new int[](2);
28852892
assert(a.length == 2);

0 commit comments

Comments
 (0)