forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.dd
255 lines (204 loc) · 6.97 KB
/
property.dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
Ddoc
$(SPEC_S Properties,
$(P Every type and expression has properties that can be queried:)
$(TABLE2 Property Examples,
$(TR $(TH Expression) $(TH Value))
$(TR $(TD int.sizeof) $(TD yields 4))
$(TR $(TD float.nan) $(TD yields the floating point nan (Not A Number) value))
$(TR $(TD (float).nan) $(TD yields the floating point nan value))
$(TR $(TD (3).sizeof) $(TD yields 4 (because 3 is an int)))
$(TR $(TD 2.sizeof) $(TD syntax error, since "2." is a floating point number))
$(TR $(TD int.init) $(TD default initializer for int's))
$(TR $(TD int.mangleof) $(TD yields the string "i"))
$(TR $(TD int.stringof) $(TD yields the string "int"))
$(TR $(TD (1+2).stringof) $(TD yields the string "1 + 2"))
)
$(BR)
$(TABLE2 Properties for All Types,
$(TR $(TH Property) $(TH Description))
$(TR $(TD $(LINK2 #init, .init)) $(TD initializer))
$(TR $(TD $(LINK2 #sizeof, .sizeof)) $(TD size in bytes (equivalent to C's sizeof(type))))
$(TR $(TD $(LINK2 #alignof, .alignof)) $(TD alignment size))
$(TR $(TD $(LINK2 #mangleof, .mangleof)) $(TD string representing the $(SINGLEQUOTE mangled) representation of the type))
$(TR $(TD $(LINK2 #stringof, .stringof)) $(TD string representing the source representation of the type))
)
$(BR)
$(TABLE2 Properties for Integral Types,
$(TR $(TH Property) $(TH Description))
$(TR $(TD .init) $(TD initializer (0)))
$(TR $(TD .max) $(TD maximum value))
$(TR $(TD .min) $(TD minimum value))
)
$(BR)
$(TABLE2 Properties for Floating Point Types,
$(TR $(TH Property) $(TH Description))
$(TR $(TD .init) $(TD initializer (NaN)))
$(TR $(TD .infinity) $(TD infinity value))
$(TR $(TD .nan) $(TD NaN value))
$(TR $(TD .dig) $(TD number of decimal digits of precision))
$(TR $(TD .epsilon) $(TD smallest increment to the value 1))
$(TR $(TD .mant_dig) $(TD number of bits in mantissa))
$(TR $(TD .max_10_exp) $(TD maximum int value such that 10<sup>max_10_exp</sup> is representable))
$(TR $(TD .max_exp) $(TD maximum int value such that 2<sup>max_exp-1</sup> is representable))
$(TR $(TD .min_10_exp) $(TD minimum int value such that 10<sup>min_10_exp</sup> is representable as a normalized value))
$(TR $(TD .min_exp) $(TD minimum int value such that 2<sup>min_exp-1</sup> is representable as a normalized value))
$(TR $(TD .max) $(TD largest representable value that's not infinity))
$(V1 $(TR $(TD .min) $(TD smallest representable normalized value that's not 0)))
$(V2 $(TR $(TD .min_normal) $(TD smallest representable normalized value that's not 0)))
$(TR $(TD .re) $(TD real part))
$(TR $(TD .im) $(TD imaginary part))
)
$(BR)
$(TABLE2 Properties for Class Types,
$(TR $(TH Property) $(TH Description))
$(TR $(TD $(LINK2 #classinfo, .classinfo)) $(TD Information about the dynamic type of the class))
)
$(SECTION2 $(LNAME2 init, .init) Property,
$(P $(B .init) produces a constant expression that is the default
initializer. If applied to a type, it is the default initializer
for that type. If applied to a variable or field, it is the
default initializer for that variable or field.
For example:
)
----------------
int a;
int b = 1;
typedef int t = 2;
t c;
t d = cast(t)3;
int.init // is 0
a.init // is 0
b.init // is 0
t.init // is 2
c.init // is 2
d.init // is 2
struct Foo
{
int a;
int b = 7;
}
Foo.a.init // is 0
Foo.b.init // is 7
----------------
)
$(SECTION2 $(LNAME2 stringof, .stringof) Property,
$(P $(B .stringof) produces a constant string that is the
source representation of its prefix.
If applied to a type, it is the string for that type.
If applied to an expression, it is the source representation
of that expression. Semantic analysis is not done
for that expression.
For example:
)
----------------
struct Foo { }
enum Enum { RED }
typedef int myint;
void main()
{
writefln((1+2).stringof); // "1 + 2"
writefln(Foo.stringof); // "Foo"
writefln(test.Foo.stringof); // "test.Foo"
writefln(int.stringof); // "int"
writefln((int*[5][]).stringof); // "int*[5][]"
writefln(Enum.RED.stringof); // "Enum.RED"
writefln(test.myint.stringof); // "test.myint"
writefln((5).stringof); // "5"
}
----------------
)
$(SECTION2 $(LNAME2 sizeof, .sizeof Property),
$(P $(CODE $(I e).sizeof) gives the size in bytes of the expression
$(I e).
)
$(P When getting the size of a member, it is not necessary for
there to be a $(I this) object:
)
---
struct S {
int a;
static int foo() {
return a.sizeof; // returns 4
}
}
void test() {
int x = S.a.sizeof; // sets x to 4
}
---
$(P $(CODE .sizeof) applied to a class object returns the size of
the class reference, not the class instantiation.)
)
$(SECTION2 $(LNAME2 alignof, .alignof Property),
$(P $(CODE .alignof) gives the aligned size of an expression or type.
For example, an aligned size of 1 means that it is aligned on
a byte boundary, 4 means it is aligned on a 32 bit boundary.
)
)
$(SECTION2 $(LNAME2 classinfo, .classinfo) Property,
$(P $(CODE .classinfo) provides information about the dynamic type
of a class object.
$(V1 It returns a reference to type $(LINK2 phobos/object.html, object.ClassInfo).)
$(V2 It returns a reference to type $(LINK2 phobos/object.html#TypeInfo_Class, object.TypeInfo_Class).)
)
)
$(SECTION3 $(LNAME2 classproperties, User Defined Class and Struct Properties),
$(P Properties are member functions that can be syntactically treated
as if they were fields. Properties can be read from or written to.
A property is read by calling a method with no arguments;
a property is written by calling a method with its argument
being the value it is set to.
)
$(P A simple property would be:)
$(V1
----------------
struct Foo
{
int data() { return m_data; } // read property
int data(int value) { return m_data = value; } // write property
private:
int m_data;
}
----------------
)
$(V2
----------------
struct Foo
{
@property int data() { return m_data; } // read property
@property int data(int value) { return m_data = value; } // write property
private:
int m_data;
}
----------------
$(P Properties are marked with the $(CODE @property) attribute.
Properties may only have zero or one parameter, and may not be variadic.
Property functions may not be overloaded with non-property functions.
)
)
$(P To use it:)
----------------
int test()
{
Foo f;
f.data = 3; // same as f.data(3);
return f.data + 3; // same as return f.data() + 3;
}
----------------
$(P The absence of a read method means that the property is write-only.
The absence of a write method means that the property is read-only.
Multiple write methods can exist; the correct one is selected using
the usual function overloading rules.
)
$(P In all the other respects, these methods are like any other methods.
They can be static, have different linkages, $(V1 be overloaded with
methods with multiple parameters,) have their address taken, etc.
)
$(P $(B Note:) Properties currently cannot be the lvalue of an
$(I op)=, ++, or -- operator.
)
)
)
Macros:
TITLE=Properties
WIKI=Property
CATEGORY_SPEC=$0