@@ -6,21 +6,6 @@ $(HEADERNAV_TOC)
6
6
7
7
$(P Every symbol, type, and expression has properties that can be queried:)
8
8
9
- $(TABLE2 Property Examples,
10
- $(THEAD Expression, Value)
11
- $(TROW $(D int.sizeof), yields 4)
12
- $(TROW $(D float.nan), yields the floating point nan (Not A Number) value)
13
- $(TROW $(D (float).nan), yields the floating point nan value)
14
- $(TROW $(D (3).sizeof), yields 4 (because 3 is an int))
15
-
16
- $(TROW $(D int.init), default initializer for ints)
17
- $(TROW $(D int.mangleof), yields the string "i")
18
- $(TROW $(D int.stringof), yields the string "int")
19
- $(TROW $(D (1+2).stringof), yields the string "1 + 2")
20
- )
21
-
22
- $(BR)
23
-
24
9
$(TABLE2 Properties for All Types,
25
10
$(THEAD Property, Description)
26
11
$(TROW $(RELATIVE_LINK2 init, $(D .init)), initializer)
@@ -30,11 +15,21 @@ $(TROW $(RELATIVE_LINK2 mangleof, $(D .mangleof)), string representing the $(SIN
30
15
$(TROW $(RELATIVE_LINK2 stringof, $(D .stringof)), string representing the source representation of the type)
31
16
)
32
17
18
+ $(TABLE2 Examples,
19
+ $(THEAD Expression, Value)
20
+ $(TROW $(D int.sizeof), yields 4)
21
+ $(TROW $(D (3).sizeof), yields 4 (because 3 is an int))
22
+
23
+ $(TROW $(D int.init), yields 0)
24
+ $(TROW $(D int.mangleof), yields the string "i")
25
+ $(TROW $(D int.stringof), yields the string "int")
26
+ $(TROW $(D (1+2).stringof), yields the string "1 + 2")
27
+ )
28
+
33
29
$(BR)
34
30
35
31
$(TABLE2 Properties for Integral Types,
36
32
$(THEAD Property, Description)
37
- $(TROW $(D .init), initializer)
38
33
$(TROW $(D .max), maximum value)
39
34
$(TROW $(D .min), minimum value)
40
35
)
43
38
44
39
$(TABLE_2COLS Properties for Floating Point Types,
45
40
$(THEAD Property, Description)
46
- $(TROW $(D .init), initializer (NaN))
47
41
$(TROW $(D .infinity), infinity value)
48
- $(TROW $(D .nan), NaN value)
42
+ $(TROW $(D .nan), NaN - $(HTTPS en.wikipedia.org/wiki/NaN, Not a Number) value
43
+ (other NaN values can be produced))
49
44
$(TROW $(D .dig), number of decimal digits of precision)
50
45
$(TROW $(D .epsilon), smallest increment to the value 1)
51
46
$(TROW $(D .mant_dig), number of bits in mantissa)
@@ -60,47 +55,74 @@ $(TROW $(D .re), real part)
60
55
$(TROW $(D .im), imaginary part)
61
56
)
62
57
58
+ $(TABLE2 Floating Point Examples,
59
+ $(THEAD Expression, Value)
60
+ $(TROW $(D float.nan), yields the floating point NaN value)
61
+ $(TROW $(D (2.5F).nan), yields the floating point NaN value)
62
+ )
63
+
63
64
$(BR)
64
65
65
66
$(TABLE2 Properties for Class Types,
66
67
$(THEAD Property, Description)
67
68
$(TROW $(RELATIVE_LINK2 classinfo, $(D .classinfo)), Information about the dynamic type of the class)
68
69
)
69
70
71
+ $(P See also: $(DDSUBLINK spec/enum, enum_properties, Enum Properties).)
72
+
73
+
70
74
$(H2 $(LNAME2 init, .init Property))
71
75
72
76
$(P $(D .init) produces a constant expression that is the default
73
77
initializer. If applied to a type, it is the default initializer
74
78
for that type. If applied to a variable or field, it is the
75
79
default initializer for that variable or field's type.
80
+ The default values for different kinds of types are described below:
76
81
)
77
82
83
+ $(TABLE
84
+ $(THEAD Type, `.init` Value)
85
+ $(TROW `char`, `'\xff'`)
86
+ $(TROW `dchar`, `'\xffff'`)
87
+ $(TROW `wchar`, `'\xffff'`)
88
+ $(TROW Enum, first member value)
89
+ $(TROW Integers, `0`)
90
+ $(TROW Floating Point, NaN)
91
+ $(TROW Reference Types, `null`)
92
+ $(TROW Structs, each $(DDSUBLINK spec/struct, static_struct_init, field's default value))
93
+ $(TROW Unions, first member value)
94
+ )
95
+
96
+ $(RATIONALE Where possible, the default value for a type is an invalid value.)
97
+
98
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
78
99
----------------
79
100
int a;
80
101
int b = 1;
81
102
82
- int.init // is 0
83
- a.init // is 0
84
- b.init // is 0
103
+ static assert( int.init == 0);
104
+ static assert( a.init == 0);
105
+ static assert( b.init == 0);
85
106
86
107
struct Foo
87
108
{
88
109
int a;
89
110
int b = 7;
90
111
}
91
112
92
- Foo.init.a // is 0
93
- Foo.init.b // is 7
113
+ static assert( Foo.init.a == 0);
114
+ static assert( Foo.init.b == 7);
94
115
----------------
95
-
96
- $(P $(B Note:) $(D .init) produces a default initialized object, not
97
- default constructed. If there is a default constructor for an object,
116
+ )
117
+ $(P Note that $(D .init) produces a default initialized object, not a
118
+ default constructed one . If there is a default constructor for an object,
98
119
it may produce a different value.)
99
120
100
121
$(OL
101
122
$(LI If $(D T) is a nested struct, the context pointer in $(D T.init)
102
123
is $(D null).)
103
124
125
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
104
126
----------------
105
127
void main()
106
128
{
@@ -115,10 +137,11 @@ void main()
115
137
s3.foo(); // Access violation
116
138
}
117
139
----------------
118
-
140
+ )
119
141
$(LI If $(D T) is a struct which has $(CODE @disable this();), $(D T.init)
120
142
might return a logically incorrect object.)
121
143
144
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
122
145
----------------
123
146
struct S
124
147
{
@@ -138,8 +161,10 @@ void main()
138
161
s3.check(); // Assertion failure
139
162
}
140
163
----------------
164
+ )
141
165
)
142
166
167
+
143
168
$(H2 $(LNAME2 stringof, .stringof Property))
144
169
145
170
$(P $(D .stringof) produces a constant string that is the
0 commit comments