Skip to content

Commit 90b6ea2

Browse files
authored
[spec] Tweak enum & type docs (#3683)
* [spec] Tweak enum & type docs Move *Enum Default Initializer* section into *Enum Properties*. Add link to enum operations. Add Enum Operations subheading and example. Add *Preserving Bit Patterns* subheading, use list. * Extend example to show errors * Add *Enum Variables* subheading Link to final switch. * Preserve default initializer anchor * Use list for items about named enums and types
1 parent 7e89d59 commit 90b6ea2

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

spec/enum.dd

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,15 @@ $(H2 $(LNAME2 named_enums, Named Enums))
7070
---
7171

7272

73-
$(P If the $(I EnumBaseType) is not explicitly set, and the first
73+
$(P If the $(GLINK EnumBaseType) is not explicitly set, and the first
7474
$(I EnumMember) has an *AssignExpression*, it is set to the type of that
7575
*AssignExpression*. Otherwise, it defaults to
7676
type $(CODE int).)
7777

78-
$(P Named enum members may not have individual $(I Type)s.
79-
)
80-
81-
$(P A named enum member can be implicitly cast to its $(GLINK EnumBaseType),
82-
but $(I EnumBaseType) types
83-
cannot be implicitly cast to an enum type.
84-
)
78+
* A named enum member can be
79+
$(DDSUBLINK spec/type, implicit-conversions, implicitly cast to its $(I EnumBaseType)).
80+
* An $(I EnumBaseType) instance cannot be implicitly cast to a named enum type.
81+
* A named enum member does not have an individual $(I Type).
8582

8683
$(P The value of an $(GLINK EnumMember) is given by its *AssignExpression* if present.
8784
If there is no *AssignExpression* and it is the first $(I EnumMember),
@@ -129,17 +126,26 @@ enum E : C
129126
writeln(X.init); // error: enum X is opaque and has no default initializer
130127
---
131128

132-
$(H3 $(LNAME2 enum_default_initializer, Enum Default Initializer))
129+
$(H3 $(LEGACY_LNAME2 enum_default_initializer, enum_variables, Enum Variables))
133130

134-
$(P The $(CODE .init) property of an enum type is the value
135-
of the first member of that enum.
136-
This is also the default initializer for the enum type.
131+
$(P A variable can be of named enum type.
132+
The default initializer is the first member defined for the enum type.
137133
)
138134

135+
$(SPEC_RUNNABLE_EXAMPLE_RUN
139136
------
140137
enum X { A=3, B, C }
141-
X x; // x is initialized to 3
138+
X x;
139+
assert(x == X.A);
140+
x |= X.B;
141+
assert(x & X.A);
142142
------
143+
)
144+
145+
$(P The result type of a binary operation performed when the operands have
146+
different types is defined $(DDSUBLINK spec/type, enum-ops, here).)
147+
148+
$(P See also: $(DDSUBLINK spec/statement, final-switch-statement, `final switch`).)
143149

144150
$(H3 $(LNAME2 enum_properties, Enum Properties))
145151

spec/type.dd

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,10 @@ $(H3 $(LEGACY_LNAME2 Usual Arithmetic Conversions, usual-arithmetic-conversions,
432432
---
433433
)
434434

435-
$(P If one or both of the operand types is an enum after
436-
undergoing the above conversions, the result type is:)
435+
$(H4 $(LNAME2 enum-ops, Enum Operations))
436+
437+
$(P If one or both of the operand types is an $(DDLINK spec/enum, Enums, enum) after
438+
undergoing the above conversions, the result type is determined as follows:)
437439

438440
$(OL
439441
$(LI If the operands are the same type, the result will be of
@@ -446,6 +448,32 @@ $(H3 $(LEGACY_LNAME2 Usual Arithmetic Conversions, usual-arithmetic-conversions,
446448
original type.)
447449
)
448450

451+
$(SPEC_RUNNABLE_EXAMPLE_COMPILE
452+
---
453+
enum E { a, b, c }
454+
enum F { x, y }
455+
456+
void test()
457+
{
458+
E e = E.a;
459+
e = e | E.c;
460+
//e = e + 4; // error, can't assign int to E
461+
int i = e + 4;
462+
e += 4; // OK, see below
463+
464+
F f;
465+
//f = e | f; // error, can't assign int to F
466+
i = e | f;
467+
}
468+
---
469+
)
470+
471+
$(NOTE Above, `e += 4` compiles because the
472+
$(DDSUBLINK spec/expression, assignment_operator_expressions, operator assignment)
473+
is equivalent to `e = cast(E)(e + 4)`.)
474+
475+
$(H3 $(LNAME2 disallowed-conversions, Preserving Bit Patterns))
476+
449477
$(P Integer values cannot be implicitly converted to another
450478
type that cannot represent the integer bit pattern after integral
451479
promotion. For example:)
@@ -459,12 +487,12 @@ ulong u4 = long(-1); // ok, -1 can be represented in a long, which can be conve
459487
---
460488
)
461489

462-
$(P Floating point types cannot be implicitly converted to
463-
integral types. Complex or imaginary floating point types cannot be implicitly converted
464-
to non-complex floating point types. Non-complex floating point types
465-
cannot be implicitly converted to imaginary floating
466-
point types.
467-
)
490+
* Floating point types cannot be implicitly converted to
491+
integral types.
492+
* Complex or imaginary floating point types cannot be implicitly converted
493+
to non-complex floating point types.
494+
* Non-complex floating point types cannot be implicitly converted to imaginary floating
495+
point types.
468496

469497
$(H3 $(LNAME2 vrp, Value Range Propagation))
470498

0 commit comments

Comments
 (0)