@@ -4,7 +4,7 @@ $(SPEC_S Properties,
4
4
5
5
$(HEADERNAV_TOC)
6
6
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:)
8
8
9
9
$(TABLE2 Property Examples,
10
10
$(THEAD Expression, Value)
@@ -24,17 +24,17 @@ $(BR)
24
24
$(TABLE2 Properties for All Types,
25
25
$(THEAD Property, Description)
26
26
$(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)
28
28
$(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)
30
30
$(TROW $(RELATIVE_LINK2 stringof, $(D .stringof)), string representing the source representation of the type)
31
31
)
32
32
33
33
$(BR)
34
34
35
35
$(TABLE2 Properties for Integral Types,
36
36
$(THEAD Property, Description)
37
- $(TROW $(D .init), initializer (0) )
37
+ $(TROW $(D .init), initializer)
38
38
$(TROW $(D .max), maximum value)
39
39
$(TROW $(D .min), minimum value)
40
40
)
@@ -73,22 +73,15 @@ $(H2 $(LNAME2 init, .init Property))
73
73
initializer. If applied to a type, it is the default initializer
74
74
for that type. If applied to a variable or field, it is the
75
75
default initializer for that variable or field's type.
76
- For example:
77
76
)
78
77
79
78
----------------
80
79
int a;
81
80
int b = 1;
82
- typedef int t = 2;
83
- t c;
84
- t d = cast(t)3;
85
81
86
82
int.init // is 0
87
83
a.init // is 0
88
84
b.init // is 0
89
- t.init // is 2
90
- c.init // is 2
91
- d.init // is 2
92
85
93
86
struct Foo
94
87
{
@@ -100,8 +93,9 @@ Foo.init.a // is 0
100
93
Foo.init.b // is 7
101
94
----------------
102
95
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.)
105
99
106
100
$(OL
107
101
$(LI If $(D T) is a nested struct, the context pointer in $(D T.init)
@@ -110,14 +104,14 @@ Foo.init.b // is 7
110
104
----------------
111
105
void main()
112
106
{
113
- int a ;
107
+ int x ;
114
108
struct S
115
109
{
116
- void foo() { a = 1; } // access a variable in enclosing scope
110
+ void foo() { x = 1; } // access x in enclosing scope via context pointer
117
111
}
118
- S s1; // OK. S() correctly initialize its frame pointer.
112
+ S s1; // OK. S() correctly initialize its context pointer.
119
113
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
121
115
s3.foo(); // Access violation
122
116
}
123
117
----------------
@@ -128,19 +122,20 @@ void main()
128
122
----------------
129
123
struct S
130
124
{
131
- int a ;
125
+ int x ;
132
126
@disable this();
133
- this(int n) { a = n; }
134
- invariant { assert(a > 0); }
127
+ this(int n) { x = n; }
128
+ invariant { assert(x > 0); }
135
129
void check() {}
136
130
}
131
+
137
132
void main()
138
133
{
139
134
//S s1; // Error: variable s1 initializer required for type S
140
135
//S s2 = S(); // Error: constructor S.this is not callable
141
136
// 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
144
139
}
145
140
----------------
146
141
)
@@ -153,39 +148,35 @@ $(H2 $(LNAME2 stringof, .stringof Property))
153
148
If applied to an expression, it is the source representation
154
149
of that expression. Semantic analysis is not done
155
150
for that expression.
156
- For example:
157
151
)
158
152
159
- ----------------
160
- module test;
161
- import std.stdio;
162
-
163
- struct Foo { }
153
+ ---
154
+ module test;
155
+ import std.stdio;
164
156
165
- enum Enum { RED }
157
+ struct Dog { }
166
158
167
- typedef int myint;
159
+ enum Color { Red }
168
160
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
+ ---
181
172
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.)
185
175
186
- $(P Instead you should prefer to use the
176
+ $(BEST_PRACTICE Do not use `.stringof` for code generation.
177
+ Instead use the
187
178
$(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).)
189
180
190
181
$(H2 $(LNAME2 sizeof, .sizeof Property))
191
182
@@ -223,6 +214,31 @@ $(H2 $(LNAME2 alignof, .alignof Property))
223
214
a byte boundary, 4 means it is aligned on a 32 bit boundary.
224
215
)
225
216
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
+
226
242
$(H2 $(LNAME2 classinfo, .classinfo Property))
227
243
228
244
$(P $(CODE .classinfo) provides information about the dynamic type of a class
0 commit comments