Skip to content

Commit 67852fc

Browse files
ntreldlang-bot
authored andcommitted
[spec/property] Improve stringof/sizeof/alignof docs
Make stringof example runnable. Show stringof on a symbol - module name. Make it clearer stringof on expression doesn't evaluate expression. Simplify sizeof example and make runnable. Add link to classInstanceSize trait. Add link to `align` example for alignof.
1 parent 6ce897d commit 67852fc

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

spec/property.dd

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ $(H2 $(LNAME2 stringof, .stringof Property))
180180
of that expression. The expression will not be evaluated.
181181
)
182182

183+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
183184
---
184185
module test;
185186
import std.stdio;
@@ -193,17 +194,18 @@ $(H2 $(LNAME2 stringof, .stringof Property))
193194
void main()
194195
{
195196
writeln((1+2).stringof); // "1 + 2"
197+
writeln(test.stringof); // "module test"
196198
writeln(Dog.stringof); // "Dog"
197-
writeln(test.Dog.stringof); // "Dog"
198199
writeln(int.stringof); // "int"
199200
writeln((int*[5][]).stringof); // "int*[5][]"
200201
writeln(Color.Red.stringof); // "Red"
201202
writeln((5).stringof); // "5"
202203

203204
writeln((++i).stringof); // "i += 1"
204-
writeln(i); // 4
205+
assert(i == 4); // `++i` was not evaluated
205206
}
206207
---
208+
)
207209

208210
$(IMPLEMENTATION_DEFINED The string representation for a type or expression
209211
can vary.)
@@ -223,32 +225,32 @@ $(H2 $(LNAME2 sizeof, .sizeof Property))
223225
there to be a $(I this) object:
224226
)
225227

228+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
226229
---
227230
struct S
228231
{
229232
int a;
230-
static int foo()
231-
{
232-
return a.sizeof; // returns 4
233-
}
234233
}
235234

236-
void test()
237-
{
238-
int x = S.a.sizeof; // sets x to 4
239-
}
235+
static assert(S.a.sizeof == 4);
236+
static assert(Object.sizeof == (void*).sizeof);
240237
---
238+
)
241239

242240
$(P $(CODE .sizeof) applied to a class object returns the size of
243241
the class reference, not the class instantiation.)
244242

243+
$(P See also: $(DDSUBLINK spec/traits, classInstanceSize, `__traits(classInstanceSize)`).)
244+
245245
$(H2 $(LNAME2 alignof, .alignof Property))
246246

247247
$(P $(CODE .alignof) gives the aligned size of an expression or type.
248248
For example, an aligned size of 1 means that it is aligned on
249249
a byte boundary, 4 means it is aligned on a 32 bit boundary.
250250
)
251251

252+
$(P See $(DDSUBLINK spec/attribute, align, the `align` attribute) for an example.)
253+
252254
$(IMPLEMENTATION_DEFINED the actual aligned size.)
253255

254256
$(BEST_PRACTICE Be particularly careful when laying out an object that must

0 commit comments

Comments
 (0)