Skip to content

Commit 6c72655

Browse files
ntreldlang-bot
authored andcommitted
Improve union docs
Mention union fields use overlapping storage, add example. Add *Members* heading and *Union Members* subsection. Add recursive types subheading and example.
1 parent 64043b7 commit 6c72655

File tree

1 file changed

+78
-12
lines changed

1 file changed

+78
-12
lines changed

spec/struct.dd

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ $(HEADERNAV_TOC)
77
$(H2 $(LNAME2 intro, Introduction))
88

99
$(P Whereas $(DDLINK spec/class, Classes, classes) are reference types,
10-
structs are value types.
11-
Structs and unions are simple aggregations of data and their
10+
structs and unions are value types.
11+
Structs are simple aggregations of data and their
1212
associated operations on that data.
1313
)
1414

@@ -21,7 +21,8 @@ $(GNAME StructDeclaration):
2121

2222
$(GNAME AnonStructDeclaration):
2323
$(D struct) $(GLINK AggregateBody)
24-
24+
)
25+
$(GRAMMAR
2526
$(GNAME UnionDeclaration):
2627
$(D union) $(GLINK_LEX Identifier) $(D ;)
2728
$(D union) $(GLINK_LEX Identifier) $(GLINK AggregateBody)
@@ -30,7 +31,8 @@ $(GNAME UnionDeclaration):
3031

3132
$(GNAME AnonUnionDeclaration):
3233
$(D union) $(GLINK AggregateBody)
33-
34+
)
35+
$(GRAMMAR
3436
$(GNAME AggregateBody):
3537
$(D {) $(GLINK2 module, DeclDefs)$(OPT) $(D })
3638
)
@@ -44,7 +46,8 @@ struct S
4446
int i;
4547
}
4648

47-
void main() {
49+
void main()
50+
{
4851
S a;
4952
a.i = 3;
5053

@@ -60,12 +63,40 @@ void main() {
6063
by default. To allocate on the heap, use `new`:)
6164
---
6265
S* p = new S;
66+
assert(p.i == 0);
6367
---
6468

69+
$(P A struct can contain multiple fields which are stored sequentially.
70+
Conversely, multiple fields in a union use overlapping storage.)
71+
72+
$(SPEC_RUNNABLE_EXAMPLE_RUN
73+
---
74+
union U
75+
{
76+
ubyte i;
77+
char c;
78+
}
79+
80+
void main()
81+
{
82+
U u;
83+
u.i = 3;
84+
assert(u.c == '\x03');
85+
u.c++;
86+
assert(u.i == 4);
87+
}
88+
---
89+
)
90+
91+
$(H2 $(LNAME2 members, Members))
92+
93+
$(H3 $(LNAME2 struct-members, Struct Members))
94+
6595
$(P A struct definition can contain:)
6696
$(UL
67-
$(LI fields)
68-
$(LI $(DDSUBLINK spec/attribute, static, static) fields)
97+
$(LI Fields)
98+
$(LI $(DDSUBLINK spec/attribute, static, Static) fields)
99+
$(LI $(RELATIVE_LINK2 anonymous, Anonymous Structs and Unions))
69100
$(LI $(DDSUBLINK spec/class, member-functions, member functions)
70101
$(UL
71102
$(LI static member functions)
@@ -75,24 +106,59 @@ void main() {
75106
$(LI $(DDLINK spec/operatoroverloading, Operator Overloading, Operator Overloading))
76107
)
77108
$(LI $(DDSUBLINK spec/class, alias-this, Alias This))
78-
$(LI other declarations (see $(GLINK2 module, DeclDef)))
109+
$(LI Other declarations (see $(GLINK2 module, DeclDef)))
79110
)
80111
)
81112

82113
$(P A struct is defined to not have an identity; that is,
83114
the implementation is free to make bit copies of the struct
84115
as convenient.)
85116

86-
$(P Structs and unions may not contain an instance of themselves,
87-
however, they may contain a pointer to the same type.
88-
)
89-
90117
$(BEST_PRACTICE
91118
$(OL
92119
$(LI Bit fields are supported with the
93120
$(LINK2 https://dlang.org/phobos/std_bitmanip.html#bitfields, bitfields) template.)
94121
))
95122

123+
$(H3 $(LNAME2 union-members, Union Members))
124+
125+
$(P A union definition can contain:)
126+
$(UL
127+
$(LI Fields)
128+
$(LI $(DDSUBLINK spec/attribute, static, Static) fields)
129+
$(LI $(RELATIVE_LINK2 anonymous, Anonymous Structs and Unions))
130+
$(LI $(DDSUBLINK spec/class, member-functions, member functions)
131+
$(UL
132+
$(LI static member functions)
133+
$(LI $(RELATIVE_LINK2 UnionConstructor, Constructors))
134+
$(LI $(DDLINK spec/operatoroverloading, Operator Overloading, Operator Overloading))
135+
)
136+
$(LI $(DDSUBLINK spec/class, alias-this, Alias This))
137+
$(LI Other declarations (see $(GLINK2 module, DeclDef)))
138+
)
139+
)
140+
141+
$(H3 $(LNAME2 recursive-types, Recursive Structs and Unions))
142+
143+
$(P Structs and unions may not contain a non-static instance of themselves,
144+
however, they may contain a pointer to the same type.
145+
)
146+
147+
$(SPEC_RUNNABLE_EXAMPLE_FAIL
148+
---
149+
struct S
150+
{
151+
S* ptr; // OK
152+
S[] slice; // OK
153+
154+
S s; // error
155+
S[2] array; // error
156+
157+
static S global; // OK
158+
}
159+
---
160+
)
161+
96162

97163
$(H2 $(LNAME2 struct_layout, Struct Layout))
98164

0 commit comments

Comments
 (0)