Skip to content

Commit 962c5f0

Browse files
authored
Merge pull request #2121 from WalterBright/property-touchup
property.dd: minor fixes merged-on-behalf-of: Andrei Alexandrescu <andralex@users.noreply.github.com>
2 parents 32811fa + 46a34c2 commit 962c5f0

File tree

1 file changed

+63
-47
lines changed

1 file changed

+63
-47
lines changed

spec/property.dd

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $(SPEC_S Properties,
44

55
$(HEADERNAV_TOC)
66

7-
$(P Every type and expression has properties that can be queried:)
7+
$(P Every symbol, type, and expression has properties that can be queried:)
88

99
$(TABLE2 Property Examples,
1010
$(THEAD Expression, Value)
@@ -24,17 +24,17 @@ $(BR)
2424
$(TABLE2 Properties for All Types,
2525
$(THEAD Property, Description)
2626
$(TROW $(RELATIVE_LINK2 init, $(D .init)), initializer)
27-
$(TROW $(RELATIVE_LINK2 sizeof, $(D .sizeof)), size in bytes (equivalent to C's sizeof(type)))
27+
$(TROW $(RELATIVE_LINK2 sizeof, $(D .sizeof)), size in bytes)
2828
$(TROW $(RELATIVE_LINK2 alignof, $(D .alignof)), alignment size)
29-
$(TROW $(D .mangleof), string representing the $(SINGLEQUOTE mangled) representation of the type)
29+
$(TROW $(RELATIVE_LINK2 mangleof, $(D .mangleof)), string representing the $(SINGLEQUOTE mangled) representation of the type)
3030
$(TROW $(RELATIVE_LINK2 stringof, $(D .stringof)), string representing the source representation of the type)
3131
)
3232

3333
$(BR)
3434

3535
$(TABLE2 Properties for Integral Types,
3636
$(THEAD Property, Description)
37-
$(TROW $(D .init), initializer (0))
37+
$(TROW $(D .init), initializer)
3838
$(TROW $(D .max), maximum value)
3939
$(TROW $(D .min), minimum value)
4040
)
@@ -73,22 +73,15 @@ $(H2 $(LNAME2 init, .init Property))
7373
initializer. If applied to a type, it is the default initializer
7474
for that type. If applied to a variable or field, it is the
7575
default initializer for that variable or field's type.
76-
For example:
7776
)
7877

7978
----------------
8079
int a;
8180
int b = 1;
82-
typedef int t = 2;
83-
t c;
84-
t d = cast(t)3;
8581

8682
int.init // is 0
8783
a.init // is 0
8884
b.init // is 0
89-
t.init // is 2
90-
c.init // is 2
91-
d.init // is 2
9285

9386
struct Foo
9487
{
@@ -100,8 +93,9 @@ Foo.init.a // is 0
10093
Foo.init.b // is 7
10194
----------------
10295

103-
$(P $(B Note:) $(D .init) produces a default initialized object, not
104-
default constructed. That means using $(D .init) is sometimes incorrect.)
96+
$(P $(B Note:) $(D .init) produces a default initialized object, not
97+
default constructed. If there is a default constructor for an object,
98+
it may produce a different value.)
10599

106100
$(OL
107101
$(LI If $(D T) is a nested struct, the context pointer in $(D T.init)
@@ -110,14 +104,14 @@ Foo.init.b // is 7
110104
----------------
111105
void main()
112106
{
113-
int a;
107+
int x;
114108
struct S
115109
{
116-
void foo() { a = 1; } // access a variable in enclosing scope
110+
void foo() { x = 1; } // access x in enclosing scope via context pointer
117111
}
118-
S s1; // OK. S() correctly initialize its frame pointer.
112+
S s1; // OK. S() correctly initialize its context pointer.
119113
S s2 = S(); // OK. same as s1
120-
S s3 = S.init; // Bad. the frame pointer in s3 is null
114+
S s3 = S.init; // Bad. the context pointer in s3 is null
121115
s3.foo(); // Access violation
122116
}
123117
----------------
@@ -128,19 +122,20 @@ void main()
128122
----------------
129123
struct S
130124
{
131-
int a;
125+
int x;
132126
@disable this();
133-
this(int n) { a = n; }
134-
invariant { assert(a > 0); }
127+
this(int n) { x = n; }
128+
invariant { assert(x > 0); }
135129
void check() {}
136130
}
131+
137132
void main()
138133
{
139134
//S s1; // Error: variable s1 initializer required for type S
140135
//S s2 = S(); // Error: constructor S.this is not callable
141136
// because it is annotated with @disable
142-
S s3 = S.init; // Bad. s3.a == 0, and it violates the invariant of S.
143-
s3.check(); // Assertion failure.
137+
S s3 = S.init; // Bad. s3.x == 0, and it violates the invariant of S
138+
s3.check(); // Assertion failure
144139
}
145140
----------------
146141
)
@@ -153,39 +148,35 @@ $(H2 $(LNAME2 stringof, .stringof Property))
153148
If applied to an expression, it is the source representation
154149
of that expression. Semantic analysis is not done
155150
for that expression.
156-
For example:
157151
)
158152

159-
----------------
160-
module test;
161-
import std.stdio;
162-
163-
struct Foo { }
153+
---
154+
module test;
155+
import std.stdio;
164156

165-
enum Enum { RED }
157+
struct Dog { }
166158

167-
typedef int myint;
159+
enum Color { Red }
168160

169-
void main()
170-
{
171-
writeln((1+2).stringof); // "1 + 2"
172-
writeln(Foo.stringof); // "Foo"
173-
writeln(test.Foo.stringof); // "Foo"
174-
writeln(int.stringof); // "int"
175-
writeln((int*[5][]).stringof); // "int*[5u][]"
176-
writeln(Enum.RED.stringof); // "cast(enum)0"
177-
writeln(test.myint.stringof); // "myint"
178-
writeln((5).stringof); // "5"
179-
}
180-
----------------
161+
void main()
162+
{
163+
writeln((1+2).stringof); // "1 + 2"
164+
writeln(Dog.stringof); // "Dog"
165+
writeln(test.Dog.stringof); // "Dog"
166+
writeln(int.stringof); // "int"
167+
writeln((int*[5][]).stringof); // "int*[5u][]"
168+
writeln(Color.Red.stringof); // "cast(Color)0"
169+
writeln((5).stringof); // "5"
170+
}
171+
---
181172

182-
$(P $(B Note): Using $(D .stringof) for code generation is not recommended,
183-
as the internal representation of a type or expression can change between
184-
different compiler versions.)
173+
$(IMPLEMENTATION_DEFINED The string representation for a type or expression
174+
can vary.)
185175

186-
$(P Instead you should prefer to use the
176+
$(BEST_PRACTICE Do not use `.stringof` for code generation.
177+
Instead use the
187178
$(DDSUBLINK spec/traits, identifier, identifier) trait,
188-
or one of the Phobos helper functions such as $(PHOBOS traits, fullyQualifiedName, fullyQualifiedName).)
179+
or one of the Phobos helper functions such as $(PHOBOS traits, fullyQualifiedName, std.traits.fullyQualifiedName).)
189180

190181
$(H2 $(LNAME2 sizeof, .sizeof Property))
191182

@@ -223,6 +214,31 @@ $(H2 $(LNAME2 alignof, .alignof Property))
223214
a byte boundary, 4 means it is aligned on a 32 bit boundary.
224215
)
225216

217+
$(IMPLEMENTATION_DEFINED the actual aligned size.)
218+
219+
$(BEST_PRACTICE Be particularly careful when laying out an object that must
220+
line up with an externally imposed layout. Data misalignment can result in
221+
particularly pernicious bugs. It's often worth putting in an `assert` to assure
222+
it is correct.)
223+
224+
$(H2 $(LNAME2 mangleof, .mangleof Property))
225+
226+
$(P Mangling refers to how a symbol is represented in text form in the
227+
generated object file. $(CODE .mangleof) returns a string literal
228+
of the representation of the type or symbol it is applied to.
229+
The mangling of types and symbols with D linkage is defined by
230+
$(DDSUBLINK spec/abi, name_mangling, Name Mangling).
231+
)
232+
233+
$(IMPLEMENTATION_DEFINED
234+
$(OL
235+
$(LI whether a leading underscore is added to a symbol)
236+
$(LI the mangling of types and symbols with non-D linkage.
237+
For C and C++ linkage, this will typically
238+
match what the associated C or C++ compiler does.)
239+
)
240+
)
241+
226242
$(H2 $(LNAME2 classinfo, .classinfo Property))
227243

228244
$(P $(CODE .classinfo) provides information about the dynamic type of a class

0 commit comments

Comments
 (0)