Skip to content

Commit 0f8a59f

Browse files
committed
Rewrote the grammar for enumeration declarations. The prose is stil a WIP.
I've included a NOTE explaining the reason for the rewrite. I'm still unclear as to why we have a single enumeration type that blends together simple enumeration types (the ones that use "raw values") and discriminated union types.
1 parent 5e5f2e6 commit 0f8a59f

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

ReferenceManual/Declarations.rst

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Declarations
3030
declaration --> typealias-declaration
3131
declaration --> function-declaration
3232
declaration --> enum-declaration
33-
declaration --> enum-element-declaration
33+
declaration --> enum-member-declaration
3434
declaration --> struct-declaration
3535
declaration --> class-declaration
3636
declaration --> protocol-declaration
@@ -677,13 +677,35 @@ Function Signature
677677
Enumeration Declaration
678678
-----------------------
679679

680+
A :newTerm:`enumeration declaration` introduces a named, enumeration type into your program.
681+
682+
Enumeration declarations have two basic forms and are declared using the keyword ``enum``.
683+
684+
The first form allows you to declare an enumeration type that contains
685+
enumerators of any type, each of which can contain associated values
686+
and has the following form:
687+
680688
.. syntax-outline::
681689

682690
enum <#enumeration name#> {
691+
<#declarations#>
683692
case <#enumerator list 1#>
684693
case <#enumerator list 2#>(<#associated value type#>)
685694
}
686695

696+
Enumerations declared in this form are known as :newTerm:`discriminated unions`
697+
in other programming languages.
698+
699+
The body of an enumeration contains zero or more *declarations*
700+
and enumeration member declarations.
701+
These *declarations* can include computed properties,
702+
instance methods, initializers, and enumeration member declarations.
703+
In this form, enumerators consist the keyword ``case``
704+
followed by a list of
705+
706+
The second form allow you to declare an enumeration type that contains
707+
enumerators of the same basic type and has the following form:
708+
687709
.. syntax-outline::
688710

689711
enum <#enumeration name#> : <#raw value type#> {
@@ -714,17 +736,49 @@ Enumeration Declaration
714736

715737
Grammar of an enumeration declaration
716738

739+
enum-declaration --> attribute-list-OPT raw-value-style-enum | attribute-list-OPT union-style-enum
740+
741+
raw-value-style-enum --> enum-name generic-parameter-clause-OPT type-inheritance-clause raw-value-style-enum-body
742+
raw-value-style-enum-body --> ``{`` declarations-OPT raw-value-style-enum-members ``}``
743+
raw-value-style-enum-members --> raw-value-style-enum-member raw-value-style-enum-members-OPT
744+
raw-value-style-enum-member --> attribute-list-OPT raw-value-style-enumerator-list
745+
raw-value-style-enumerator-list --> raw-value-style-enumerator | raw-value-style-enumerator ``,`` raw-value-style-enumerator-list
746+
raw-value-style-enumerator --> identifier raw-value-assignment-OPT
747+
raw-value-assignment --> ``=`` literal
748+
enum-name --> identifier
749+
750+
union-style-enum --> enum-name generic-parameter-clause-OPT union-style-enum-body
751+
union-style-enum-body --> ``{`` declarations-OPT union-style-enum-members-OPT ``}``
752+
union-style-enum-members --> union-style-enum-member union-style-enum-members-OPT
753+
union-style-enum-member --> attribute-list-OPT union-style-enumerator-list
754+
union-style-enumerator-list --> union-style-enumerator | union-style-enumerator ``,`` union-style-enumerator-list
755+
union-style-enumerator --> identifier tuple-type-OPT
756+
757+
.. NOTE: The two types of enums are sufficiently different enough to warrant separating
758+
the grammar accordingly. ([Contributor 6004] pointed this out in his email.)
759+
I'm not sure I'm happy with the names I've chosen for two kinds of enums,
760+
so please let me know if you can think of better names!
761+
I chose union-style-enum, because this kind of enum behaves like a discriminated union,
762+
not like an ordinary enum type. They are a kind of "sum" type in the language
763+
of ADTs (Algebraic Data Types). Functional languages, like F# for example,
764+
actually have both types (discriminated unions and enumeration types),
765+
because they behave differently. I'm not sure why we've blended them together,
766+
especially given that they have distinct syntactic declaration requirements
767+
and they behave differently.
768+
769+
.. old-grammar
770+
Grammar of an enumeration declaration
771+
717772
enum-declaration --> attribute-list-OPT ``enum`` enum-name generic-parameter-clause-OPT type-inheritance-clause-OPT enum-body
718773
enum-name --> identifier
719774
enum-body --> ``{`` declarations-OPT ``}``
720775
721-
enum-element-declaration --> attribute-list-OPT ``case`` enumerator-list
776+
enum-member-declaration --> attribute-list-OPT ``case`` enumerator-list
722777
enumerator-list --> enumerator raw-value-assignment-OPT | enumerator raw-value-assignment-OPT ``,`` enumerator-list
723778
enumerator --> enumerator-name tuple-type-OPT
724779
enumerator-name --> identifier
725780
raw-value-assignment --> ``=`` literal
726781
727-
.. NOTE: You can have other declarations like methods inside of an enum declaration (e.g., methods, etc.).
728782
729783
730784
.. _Declarations_StructureDeclaration:

0 commit comments

Comments
 (0)