Skip to content

Commit 2e829c5

Browse files
authored
[spec/expression] Tweak PrimaryExpression docs (#3376)
* [spec/expression] Move NewExpression under ImportExpression As it is part of PrimaryExpression, not UnaryExpression. * [spec/expression] Fix PrimaryExpression subheadings Make Casting array literals a subheading of array literals. Add links to GC, scope & obj.new syntax for NewExpression. Make allocating Multidimensional Arrays a subheading of NewExpression. Make Special Keywords a subheading of PrimaryExpression, following the grammar.
1 parent ec499ba commit 2e829c5

File tree

1 file changed

+69
-65
lines changed

1 file changed

+69
-65
lines changed

spec/expression.dd

Lines changed: 69 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -988,69 +988,6 @@ $(GNAME ComplementExpression):
988988
prior to the complement operation.
989989
)
990990

991-
$(H3 $(LNAME2 new_expressions, New Expressions))
992-
993-
$(GRAMMAR
994-
$(GNAME NewExpression):
995-
$(D new) $(GLINK2 type, Type)
996-
$(D new) $(GLINK2 type, Type) $(D [) $(GLINK AssignExpression) $(D ])
997-
$(D new) $(GLINK2 type, Type) $(D $(LPAREN)) $(GLINK ArgumentList)$(OPT) $(D $(RPAREN))
998-
$(GLINK2 class, NewAnonClassExpression)
999-
1000-
$(GNAME ArgumentList):
1001-
$(GLINK AssignExpression)
1002-
$(GLINK AssignExpression) $(D ,)
1003-
$(GLINK AssignExpression) $(D ,) $(GSELF ArgumentList)
1004-
)
1005-
1006-
$(P $(I NewExpression)s are used to allocate memory on the garbage
1007-
collected heap.
1008-
)
1009-
1010-
$(P If a $(I NewExpression) is used as an initializer for
1011-
a function local variable with $(D scope) storage class,
1012-
then the instance is allocated on the stack.
1013-
)
1014-
1015-
$(H3 $(LNAME2 new_multidimensional, Multidimensional Arrays))
1016-
1017-
$(P To allocate multidimensional arrays, the declaration reads
1018-
in the same order as the prefix array declaration order.)
1019-
1020-
-------------
1021-
char[][] foo; // dynamic array of strings
1022-
...
1023-
foo = new char[][30]; // allocate array of 30 strings
1024-
-------------
1025-
1026-
$(P The above allocation can also be written as:)
1027-
1028-
-------------
1029-
foo = new char[][](30); // allocate array of 30 strings
1030-
-------------
1031-
1032-
$(P To allocate the nested arrays, multiple arguments can be used:)
1033-
1034-
---------------
1035-
int[][][] bar;
1036-
...
1037-
bar = new int[][][](5, 20, 30);
1038-
---------------
1039-
1040-
The code above is equivalent to:
1041-
1042-
----------
1043-
bar = new int[][][5];
1044-
foreach (ref a; bar)
1045-
{
1046-
a = new int[][20];
1047-
foreach (ref b; a)
1048-
{
1049-
b = new int[30];
1050-
}
1051-
}
1052-
-----------
1053-
1054991
$(H3 $(LNAME2 delete_expressions, Delete Expressions))
1055992

1056993
$(GRAMMAR
@@ -1773,7 +1710,7 @@ $(GNAME ArrayMemberInitialization):
17731710
---
17741711
)
17751712

1776-
$(H3 $(LNAME2 cast_array_literal, Casting))
1713+
$(H4 $(LNAME2 cast_array_literal, Casting))
17771714

17781715
$(P When array literals are cast to another array type, each
17791716
element of the array is cast to the new element type.
@@ -2272,6 +2209,73 @@ $(GNAME ImportExpression):
22722209
}
22732210
---
22742211

2212+
$(H3 $(LNAME2 new_expressions, New Expressions))
2213+
2214+
$(GRAMMAR
2215+
$(GNAME NewExpression):
2216+
$(D new) $(GLINK2 type, Type)
2217+
$(D new) $(GLINK2 type, Type) $(D [) $(GLINK AssignExpression) $(D ])
2218+
$(D new) $(GLINK2 type, Type) $(D $(LPAREN)) $(GLINK ArgumentList)$(OPT) $(D $(RPAREN))
2219+
$(GLINK2 class, NewAnonClassExpression)
2220+
2221+
$(GNAME ArgumentList):
2222+
$(GLINK AssignExpression)
2223+
$(GLINK AssignExpression) $(D ,)
2224+
$(GLINK AssignExpression) $(D ,) $(GSELF ArgumentList)
2225+
)
2226+
2227+
$(P $(I NewExpression)s are used to allocate memory on the
2228+
$(DDLINK spec/garbage, Garbage Collection, garbage
2229+
collected) heap.
2230+
)
2231+
2232+
$(P If a $(I NewExpression) is used as an initializer for
2233+
a function local variable with $(DDSUBLINK spec/attribute, scope, `scope`) storage class,
2234+
then the instance is allocated on the stack.
2235+
)
2236+
2237+
$(P `new` can also be used to allocate a
2238+
$(DDSUBLINK spec/class, nested-explicit, nested class).)
2239+
2240+
$(H4 $(LNAME2 new_multidimensional, Multidimensional Arrays))
2241+
2242+
$(P To allocate multidimensional arrays, the declaration reads
2243+
in the same order as the prefix array declaration order.)
2244+
2245+
-------------
2246+
char[][] foo; // dynamic array of strings
2247+
...
2248+
foo = new char[][30]; // allocate array of 30 strings
2249+
-------------
2250+
2251+
$(P The above allocation can also be written as:)
2252+
2253+
-------------
2254+
foo = new char[][](30); // allocate array of 30 strings
2255+
-------------
2256+
2257+
$(P To allocate the nested arrays, multiple arguments can be used:)
2258+
2259+
---------------
2260+
int[][][] bar;
2261+
...
2262+
bar = new int[][][](5, 20, 30);
2263+
---------------
2264+
2265+
The code above is equivalent to:
2266+
2267+
----------
2268+
bar = new int[][][5];
2269+
foreach (ref a; bar)
2270+
{
2271+
a = new int[][20];
2272+
foreach (ref b; a)
2273+
{
2274+
b = new int[30];
2275+
}
2276+
}
2277+
-----------
2278+
22752279
$(H3 $(LNAME2 typeid_expressions, Typeid Expressions))
22762280

22772281
$(GRAMMAR
@@ -2710,7 +2714,7 @@ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
27102714
)
27112715

27122716

2713-
$(H2 $(LNAME2 specialkeywords, Special Keywords))
2717+
$(H3 $(LNAME2 specialkeywords, Special Keywords))
27142718

27152719
$(GRAMMAR
27162720
$(GNAME SpecialKeyword):

0 commit comments

Comments
 (0)