Skip to content

Commit 2dd8036

Browse files
ntreldlang-bot
authored andcommitted
[spec/property] Improve docs
Move examples below descriptions. Move `.init` values to `.init` section, add values for other types and short rationale. Add link to enum properties. Add link for NaN and mention other bit-patterns. Make examples compilable.
1 parent 508ad62 commit 2dd8036

File tree

1 file changed

+52
-27
lines changed

1 file changed

+52
-27
lines changed

spec/property.dd

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,6 @@ $(HEADERNAV_TOC)
66

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

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-
249
$(TABLE2 Properties for All Types,
2510
$(THEAD Property, Description)
2611
$(TROW $(RELATIVE_LINK2 init, $(D .init)), initializer)
@@ -30,11 +15,21 @@ $(TROW $(RELATIVE_LINK2 mangleof, $(D .mangleof)), string representing the $(SIN
3015
$(TROW $(RELATIVE_LINK2 stringof, $(D .stringof)), string representing the source representation of the type)
3116
)
3217

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+
3329
$(BR)
3430

3531
$(TABLE2 Properties for Integral Types,
3632
$(THEAD Property, Description)
37-
$(TROW $(D .init), initializer)
3833
$(TROW $(D .max), maximum value)
3934
$(TROW $(D .min), minimum value)
4035
)
@@ -43,9 +38,9 @@ $(BR)
4338

4439
$(TABLE_2COLS Properties for Floating Point Types,
4540
$(THEAD Property, Description)
46-
$(TROW $(D .init), initializer (NaN))
4741
$(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))
4944
$(TROW $(D .dig), number of decimal digits of precision)
5045
$(TROW $(D .epsilon), smallest increment to the value 1)
5146
$(TROW $(D .mant_dig), number of bits in mantissa)
@@ -60,47 +55,74 @@ $(TROW $(D .re), real part)
6055
$(TROW $(D .im), imaginary part)
6156
)
6257

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+
6364
$(BR)
6465

6566
$(TABLE2 Properties for Class Types,
6667
$(THEAD Property, Description)
6768
$(TROW $(RELATIVE_LINK2 classinfo, $(D .classinfo)), Information about the dynamic type of the class)
6869
)
6970

71+
$(P See also: $(DDSUBLINK spec/enum, enum_properties, Enum Properties).)
72+
73+
7074
$(H2 $(LNAME2 init, .init Property))
7175

7276
$(P $(D .init) produces a constant expression that is the default
7377
initializer. If applied to a type, it is the default initializer
7478
for that type. If applied to a variable or field, it is the
7579
default initializer for that variable or field's type.
80+
The default values for different kinds of types are described below:
7681
)
7782

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
7899
----------------
79100
int a;
80101
int b = 1;
81102

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);
85106

86107
struct Foo
87108
{
88109
int a;
89110
int b = 7;
90111
}
91112

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);
94115
----------------
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,
98119
it may produce a different value.)
99120

100121
$(OL
101122
$(LI If $(D T) is a nested struct, the context pointer in $(D T.init)
102123
is $(D null).)
103124

125+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
104126
----------------
105127
void main()
106128
{
@@ -115,10 +137,11 @@ void main()
115137
s3.foo(); // Access violation
116138
}
117139
----------------
118-
140+
)
119141
$(LI If $(D T) is a struct which has $(CODE @disable this();), $(D T.init)
120142
might return a logically incorrect object.)
121143

144+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
122145
----------------
123146
struct S
124147
{
@@ -138,8 +161,10 @@ void main()
138161
s3.check(); // Assertion failure
139162
}
140163
----------------
164+
)
141165
)
142166

167+
143168
$(H2 $(LNAME2 stringof, .stringof Property))
144169

145170
$(P $(D .stringof) produces a constant string that is the

0 commit comments

Comments
 (0)