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