@@ -30,7 +30,7 @@ Declarations
30
30
declaration --> typealias-declaration
31
31
declaration --> function-declaration
32
32
declaration --> enum-declaration
33
- declaration --> enum-element -declaration
33
+ declaration --> enum-member -declaration
34
34
declaration --> struct-declaration
35
35
declaration --> class-declaration
36
36
declaration --> protocol-declaration
@@ -677,13 +677,35 @@ Function Signature
677
677
Enumeration Declaration
678
678
-----------------------
679
679
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
+
680
688
.. syntax-outline ::
681
689
682
690
enum <#enumeration name#> {
691
+ <#declarations#>
683
692
case <#enumerator list 1#>
684
693
case <#enumerator list 2#>(<#associated value type#>)
685
694
}
686
695
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
+
687
709
.. syntax-outline ::
688
710
689
711
enum <#enumeration name#> : <#raw value type#> {
@@ -714,17 +736,49 @@ Enumeration Declaration
714
736
715
737
Grammar of an enumeration declaration
716
738
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
+
717
772
enum-declaration --> attribute-list-OPT ``enum`` enum-name generic-parameter-clause-OPT type-inheritance-clause-OPT enum-body
718
773
enum-name --> identifier
719
774
enum-body --> ``{`` declarations-OPT ``}``
720
775
721
- enum-element -declaration --> attribute-list-OPT ``case `` enumerator-list
776
+ enum-member -declaration --> attribute-list-OPT ``case`` enumerator-list
722
777
enumerator-list --> enumerator raw-value-assignment-OPT | enumerator raw-value-assignment-OPT ``,`` enumerator-list
723
778
enumerator --> enumerator-name tuple-type-OPT
724
779
enumerator-name --> identifier
725
780
raw-value-assignment --> ``=`` literal
726
781
727
- .. NOTE: You can have other declarations like methods inside of an enum declaration (e.g., methods, etc.).
728
782
729
783
730
784
.. _Declarations_StructureDeclaration :
0 commit comments