Skip to content

Commit 4aa9723

Browse files
authored
[spec/struct] Clarify static initialization (#3783)
Add links to `.init`. Fix struct `.init` link to *default* struct initialization. Specify what happens when a struct member initializer without identifier follows one with an identifier (before it had to be inferred from the example). Specify that a union member initializer without identifier refers to the first member.
1 parent e8597dc commit 4aa9723

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

spec/class.dd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,9 +358,9 @@ $(GNAME Constructor):
358358
)
359359

360360
$(P Fields are by default initialized to the
361-
$(LNAME2 class-default-initializer, default initializer)
361+
$(DDSUBLINK spec/property, init, default initializer)
362362
for their type (usually 0 for integer types and
363-
NAN for floating point types).
363+
NaN for floating point types).
364364
If the field declaration has an optional $(GLINK2 declaration, Initializer)
365365
that will be used instead of the default.
366366
)

spec/property.dd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ $(TROW Enum, first member value)
8989
$(TROW Integers, `0`)
9090
$(TROW Floating Point, NaN)
9191
$(TROW Reference Types, `null`)
92-
$(TROW Structs, each $(DDSUBLINK spec/struct, static_struct_init, field's default value))
92+
$(TROW Structs, each $(DDSUBLINK spec/struct, default_struct_init, field's default value))
9393
$(TROW Unions, first member value)
9494
)
9595

spec/struct.dd

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ $(H3 $(LNAME2 default_struct_init, Default Initialization of Structs))
253253

254254
$(P Struct fields are by default initialized to whatever the
255255
$(GLINK2 declaration, Initializer) for the field is, and if none is supplied, to
256-
the default initializer for the field's type.
256+
the $(DDSUBLINK spec/property, init, default initializer) for the field's type.
257257
)
258258

259259
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
@@ -281,19 +281,22 @@ $(GNAME StructMemberInitializer):
281281
$(GLINK_LEX Identifier) $(D :) $(GLINK2 declaration, NonVoidInitializer)
282282
)
283283

284-
$(P If a $(I StructInitializer) is supplied, the
285-
fields are initialized by the $(I StructMemberInitializer) syntax.
286-
$(I StructMemberInitializers) with the $(I Identifier : NonVoidInitializer) syntax
287-
may be appear in any order, where $(I Identifier) is the field identifier.
288-
$(I StructMemberInitializer)s with the $(GLINK2 declaration, NonVoidInitializer) syntax
289-
appear in the lexical order of the fields in the $(GLINK StructDeclaration).
290-
)
284+
$(P If a $(I StructInitializer) is supplied,
285+
each $(I StructMemberInitializer) initializes a matching field:)
286+
287+
* A $(I StructMemberInitializer) using the $(I Identifier : NonVoidInitializer) syntax
288+
may appear in any order. The identifier must match a field name.
289+
* If the first $(I StructMemberInitializer) does not specify an *Identifier*,
290+
it refers to the first field in the $(GLINK StructDeclaration).
291+
* A subsequent *NonVoidInitializer* without an *Identifier* refers to the next field
292+
(in lexical order) after the one referred to in the previous *StructMemberInitializer*.
291293

292-
$(P Fields not specified in the $(I StructInitializer) are default initialized.)
294+
$(P Any field not covered by a $(I StructMemberInitializer) is default initialized.)
293295

294296
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
295297
---
296298
struct S { int a, b, c, d = 7; }
299+
297300
S r; // r.a = 0, r.b = 0, r.c = 0, r.d = 7
298301
S s = { a:1, b:2 }; // s.a = 1, s.b = 2, s.c = 0, s.d = 7
299302
S t = { c:4, b:5, a:2, d:5 }; // t.a = 2, t.b = 5, t.c = 4, t.d = 5
@@ -342,8 +345,10 @@ $(H3 $(LNAME2 default_union_init, Default Initialization of Unions))
342345

343346
$(H3 $(LNAME2 static_union_init, Static Initialization of Unions))
344347

345-
$(P Unions are initialized similarly to structs, except that only
346-
one member initializer is allowed.)
348+
$(P Unions are initialized
349+
$(RELATIVE_LINK2 static_struct_init, similarly to structs), except that only
350+
one member initializer is allowed. If the member initializer does not specify
351+
an identifier, it will initialize the first member of the union.)
347352

348353
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
349354
---

0 commit comments

Comments
 (0)