source: doc/theses/jiada_liang_MMath/relatedwork.tex @ f936e23

Last change on this file since f936e23 was f936e23, checked in by Peter A. Buhr <pabuhr@…>, 4 months ago

add more text to related-work chapter

  • Property mode set to 100644
File size: 35.2 KB
Line 
1\chapter{Related Work}
2\label{s:RelatedWork}
3
4Enumerations exist in many popular programming languages, e.g., Pascal~\cite{Pascal}, Ada~\cite{Ada}, \Csharp, \CC, Go~\cite{Go}, Java~\cite{Java}, Modula-3~\cite{Modula-3}, Rust~\cite{Rust}, Swift~\cite{Swift}, Python~\cite{Python}, and the algebraic data-type in functional programming.
5There are a large set of overlapping features among these languages, but each language has its own unique restrictions and extensions.
6
7
8\section{(Free) Pascal}
9
10\lstnewenvironment{pascal}[1][]{% necessary
11\lstset{
12language=pascal,
13escapechar=\$,                          % LaTeX escape in CFA code
14moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
15}% lstset
16\lstset{#1}% necessary
17}{}
18
19Free Pascal is a modern object-oriented version of the classic Pascal programming language.
20It allows a C-style enumeration type, where enumerators must be in assigned in ascending numerical order with a constant expression and the range can be non-consecutive.
21\begin{pascal}
22Type EnumType = ( one, two, three, forty @= 40@, fortyone );
23\end{pascal}
24Pseudo-functions @Pred@ and @Succ@ can only be used if the range is consecutive.
25The underlying type is an implementation-defined integral type large enough to hold all enumerated values; it does not have to be the smallest possible type.
26The size underlying integral type can be explicitly specified using compiler directive @$PACKENUM@~$N$, where $N$ is the number of bytes, e.g.:
27\begin{pascal}
28Type @{$\color{red}\$$PACKENUM 1}@ SmallEnum = ( one, two, three );
29            @{$\color{red}\$$PACKENUM 4}@ LargeEnum = ( BigOne, BigTwo, BigThree );
30Var S : SmallEnum; { 1 byte }
31          L : LargeEnum; { 4 bytes}
32\end{pascal}
33
34
35\section{Ada}
36
37\lstnewenvironment{ada}[1][]{% necessary
38\lstset{
39language=ada,
40escapechar=\$,                          % LaTeX escape in CFA code
41moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
42}% lstset
43\lstset{#1}% necessary
44}{}
45
46An enumeration type is defined as a list of possible values:
47\begin{ada}
48type RGB is (Red, Green, Blue);
49\end{ada}
50Like for numeric types, where e.g., 1 is an integer literal, @Red@, @Green@ and @Blue@ are called the literals of this type.
51There are no other values assignable to objects of this type.
52
53\paragraph{Operators and attributes} ~\newline
54Apart from equality (@"="@), the only operators on enumeration types are the ordering operators: @"<"@, @"<="@, @"="@, @"/="@, @">="@, @">"@, where the order relation is given implicitly by the sequence of literals:
55Each literal has a position, starting with 0 for the first, incremented by one for each successor.
56This position can be queried via the @'Pos@ attribute; the inverse is @'Val@, which returns the corresponding literal. In our example:
57\begin{ada}
58RGB'Pos (Red) = 0
59RGB'Val (0)   = Red
60\end{ada}
61There are two other important attributes: @Image@ and @Value@.
62@Image@ returns the string representation of the value (in capital letters), @Value@ is the inverse:
63\begin{ada}
64RGB'Image ( Red ) = "RED"
65RGB'Value ("Red") =  Red
66\end{ada}
67These attributes are important for simple IO (there are more elaborate IO facilities in @Ada.Text_IO@ for enumeration types).
68Note that, since Ada is case-insensitive, the string given to @'Value@ can be in any case.
69
70\paragraph{Enumeration literals} ~\newline
71Literals are overloadable, i.e. you can have another type with the same literals.
72\begin{ada}
73type Traffic_Light is (Red, Yellow, Green);
74\end{ada}
75Overload resolution within the context of use of a literal normally resolves which @Red@ is meant.
76Only if you have an unresolvable overloading conflict, you can qualify with special syntax which @Red@ is meant:
77\begin{ada}
78RGB'(Red)
79\end{ada}
80Like many other declarative items, enumeration literals can be renamed.
81In fact, such a literal is actually a function, so it has to be renamed as such:
82\begin{ada}
83function Red return P.RGB renames P.Red;
84\end{ada}
85Here, @RGB@ is assumed to be defined in package @P@, which is visible at the place of the renaming declaration.
86Renaming makes @Red@ directly visible without necessity to resort the use-clause.
87
88Note that redeclaration as a function does not affect the staticness of the literal.
89
90\paragraph{Characters as enumeration literals} ~\newline
91Rather unique to Ada is the use of character literals as enumeration literals:
92\begin{ada}
93type ABC is ('A', 'B', 'C');
94\end{ada}
95This literal @'A'@ has nothing in common with the literal @'A'@ of the predefined type @Character@ (or @Wide_Character@).
96
97Every type that has at least one character literal is a character type.
98For every character type, string literals and the concatenation operator @"&"@ are also implicitly defined.
99\begin{ada}
100type My_Character is (No_Character, 'a', Literal, 'z');
101type My_String is array (Positive range <>) of My_Character;
102
103S: My_String := "aa" & Literal & "za" & 'z';
104T: My_String := ('a', 'a', Literal, 'z', 'a', 'z');
105\end{ada}
106In this example, @S@ and @T@ have the same value.
107
108Ada's @Character@ type is defined that way.
109See Ada Programming/Libraries/Standard.
110
111\paragraph{Booleans as enumeration literals} ~\newline
112Also Booleans are defined as enumeration types:
113\begin{ada}
114type Boolean is (False, True);
115\end{ada}
116There is special semantics implied with this declaration in that objects and expressions of this type can be used as conditions.
117Note that the literals @False@ and @True@ are not Ada keywords.
118
119Thus it is not sufficient to declare a type with these literals and then hope objects of this type can be used like so:
120\begin{ada}
121type My_Boolean is (False, True);
122Condition: My_Boolean;
123
124if Condition then -- wrong, won't compile
125\end{ada}
126
127If you need your own Booleans (perhaps with special size requirements), you have to derive from the predefined Boolean:
128\begin{ada}
129type My_Boolean is new Boolean;
130Condition: My_Boolean;
131
132if Condition then -- OK
133\end{ada}
134
135\paragraph{Enumeration subtypes} ~\newline
136You can use range to subtype an enumeration type:
137\begin{ada}
138subtype Capital_Letter is Character range 'A' .. 'Z';
139type Day_Of_Week is (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);
140subtype Working_Day is Day_Of_Week range Monday .. Friday;
141\end{ada}
142
143\paragraph{Using enumerations} ~\newline
144Enumeration types being scalar subtypes, type attributes such as @First@ and @Succ@ will allow stepping through a subsequence of the values.
145\begin{ada}
146case Day_Of_Week'First is
147        when Sunday =>
148           ISO (False);
149        when Day_Of_Week'Succ(Sunday) =>
150           ISO (True);
151        when Tuesday .. Saturday =>
152           raise Program_Error;
153end case;
154\end{ada}
155A loop will automatically step through the values of the subtype's range.
156Filtering week days to include only working days with an even position number:
157\begin{ada}
158        for Day in Working_Day loop
159                if Day_Of_Week'Pos(Day) mod 2 = 0 then
160                        Work_In_Backyard;
161                end if;
162        end loop;
163\end{ada}
164Enumeration types can be used as array index subtypes, yielding a table feature:
165\begin{ada}
166type Officer_ID is range 0 .. 50;
167type Schedule is array (Working_Day) of Officer_ID;
168\end{ada}
169
170\begin{ada}
171type Subtype_Name is (Id1, Id2, Id3 ... );
172\end{ada}
173where @Id1@, @Id2@, etc. are identifiers or characters literals.
174In either case, the legal values of the type are referred to as "enumeration literals."
175Each of these values has a "position number" corresponding to its position in the list such that @Id1@ has position 0, @Id2@ has position 1, and the Nth value has position N-1.
176
177\paragraph{Attributes of Enumeration Types} ~\newline
178An enumeration type, @T@, has the following attributes: @T'First@, @T'Last@, @T'Range@, @T'Pred@, @T'Succ@, @T'Min@, @T'Max@, @T'Image@, @T'Wide_Image@, @T'Value@, @T'Wide_Value@, @T'Pos@, and @T'Val@ (pronounced "T tick first", "T tick last", etc.).
179Most of these are illustrated in the example program given below, and most of them produce what you would intuitively expect based on their names.
180
181@T'Image@ and @T'Value@ form a complementary pair of attributes.
182The former takes a value in @T@ and returns a String representation of that value.
183The latter takes a @String@ that is a representation of a value in @T@ and returns that value.
184
185@T'Pos@ and @T'Val@ form another complementary pair.
186The former takes a value in @T@ and returns its position number.
187The latter takes a position number and returns the corresponding value of type @T@.
188
189
190\section{C\raisebox{-0.7ex}{\LARGE$^\sharp$}\xspace} % latex bug: cannot use \relsize{2} so use \LARGE
191
192\lstnewenvironment{csharp}[1][]{% necessary
193\lstset{
194language=[Sharp]C,
195escapechar=\$,                          % LaTeX escape in CFA code
196moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
197}% lstset
198\lstset{#1}% necessary
199}{}
200
201An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type.
202To define an enumeration type, use the enum keyword and specify the names of enum members:
203\begin{csharp}
204enum Season {
205        Spring,
206        Summer,
207        Autumn,
208        Winter
209}
210\end{csharp}
211By default, the associated constant values of enum members are of type @int@;
212they start with zero and increase by one following the definition text order.
213
214You can explicitly specify any other integral numeric type as an underlying type of an enumeration type.
215You can also explicitly specify the associated constant values, as the following example shows:
216\begin{csharp}
217enum ErrorCode : ushort {
218        None = 0,
219        Unknown = 1,
220        ConnectionLost = 100,
221        OutlierReading = 200
222}
223\end{csharp}
224You cannot define a method inside the definition of an enumeration type.
225To add functionality to an enumeration type, create an extension method.
226
227The default value of an enumeration type @E@ is the value produced by expression @(E)0@, even if zero doesn't have the corresponding enum member.
228
229You use an enumeration type to represent a choice from a set of mutually exclusive values or a combination of choices.
230To represent a combination of choices, define an enumeration type as bit flags.
231
232\paragraph{Enumeration types as bit flags}
233
234If you want an enumeration type to represent a combination of choices, define enum members for those choices such that an individual choice is a bit field.
235That is, the associated values of those enum members should be the powers of two.
236Then, you can use the bitwise logical operators @|@ or @&@ to combine choices or intersect combinations of choices, respectively.
237To indicate that an enumeration type declares bit fields, apply the @Flags@ attribute to it.
238As the following example shows, you can also include some typical combinations in the definition of an enumeration type.
239\begin{csharp}
240[Flags]
241public enum Days {
242        None      = 0b_0000_0000// 0
243        Monday  = 0b_0000_0001// 1
244        Tuesday   = 0b_0000_0010// 2
245        Wednesday = 0b_0000_0100// 4
246        Thursday  = 0b_0000_1000// 8
247        Friday  = 0b_0001_0000// 16
248        Saturday  = 0b_0010_0000// 32
249        Sunday  = 0b_0100_0000// 64
250        Weekend   = Saturday | Sunday
251}
252
253public class FlagsEnumExample {
254        public static void Main() {
255                Days meetingDays = Days.Monday | Days.Wednesday | Days.Friday;
256                Console.WriteLine(meetingDays);
257                // Output:
258                // Monday, Wednesday, Friday
259
260                Days workingFromHomeDays = Days.Thursday | Days.Friday;
261                Console.WriteLine($\$$"Join a meeting by phone on {meetingDays & workingFromHomeDays}");
262                // Output:
263                // Join a meeting by phone on Friday
264
265                bool isMeetingOnTuesday = (meetingDays & Days.Tuesday) == Days.Tuesday;
266                Console.WriteLine($\$$"Is there a meeting on Tuesday: {isMeetingOnTuesday}");
267                // Output:
268                // Is there a meeting on Tuesday: False
269
270                var a = (Days)37;
271                Console.WriteLine(a);
272                // Output:
273                // Monday, Wednesday, Saturday
274        }
275}
276\end{csharp}
277For more information and examples, see the System.FlagsAttribute API reference page and the Non-exclusive members and the Flags attribute section of the System.Enum API reference page.
278
279\paragraph{The System.Enum type and enum constraint}
280
281The System.Enum type is the abstract base class of all enumeration types.
282It provides a number of methods to get information about an enumeration type and its values.
283For more information and examples, see the System.Enum API reference page.
284
285You can use System.Enum in a base class constraint (that is known as the enum constraint) to specify that a type parameter is an enumeration type.
286Any enumeration type also satisfies the struct constraint, which is used to specify that a type parameter is a non-nullable value type.
287Conversions
288
289For any enumeration type, there exist explicit conversions between the enumeration type and its underlying integral type.
290If you cast an enum value to its underlying type, the result is the associated integral value of an enum member.
291\begin{csharp}
292public enum Season
293{
294        Spring,
295        Summer,
296        Autumn,
297        Winter
298}
299
300public class EnumConversionExample
301{
302        public static void Main()
303        {
304                Season a = Season.Autumn;
305                Console.WriteLine($\$$"Integral value of {a} is {(int)a}")// output: Integral value of Autumn is 2
306
307                var b = (Season)1;
308                Console.WriteLine(b)// output: Summer
309
310                var c = (Season)4;
311                Console.WriteLine(c)// output: 4
312        }
313}
314\end{csharp}
315
316
317\section{\CC}
318\label{s:C++RelatedWork}
319
320\lstnewenvironment{c++}[1][]{% necessary
321\lstset{
322language=C++,
323escapechar=\$,                          % LaTeX escape in CFA code
324moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
325}% lstset
326\lstset{#1}% necessary
327}{}
328
329\CC is backwards compatible with C, so it inherited C's enumerations.
330However, the following non-backwards compatible changes have been made.
331\begin{quote}
3327.2 Change: \CC objects of enumeration type can only be assigned values of the same enumeration type.
333In C, objects of enumeration type can be assigned values of any integral type. \\
334Example:
335\begin{c++}
336enum color { red, blue, green };
337color c = 1;                                                    $\C{// valid C, invalid C++}$
338\end{c++}
339\textbf{Rationale}: The type-safe nature of \CC. \\
340\textbf{Effect on original feature}: Deletion of semantically well-defined feature. \\
341\textbf{Difficulty of converting}: Syntactic transformation. (The type error produced by the assignment can be automatically corrected by applying an explicit cast.) \\
342\textbf{How widely used}: Common.
343\end{quote}
344\begin{quote}
3457.2 Change: In \CC, the type of an enumerator is its enumeration.
346In C, the type of an enumerator is @int@. \\
347Example:
348\begin{c++}
349enum e { A };
350sizeof(A) == sizeof(int)                                $\C{// in C}$
351sizeof(A) == sizeof(e)                                  $\C{// in C++}$
352/* and sizeof(int) is not necessary equal to sizeof(e) */
353\end{c++}
354\textbf{Rationale}: In \CC, an enumeration is a distinct type. \\
355\textbf{Effect on original feature}: Change to semantics of well-defined feature. \\
356\textbf{Difficulty of converting}: Semantic transformation. \\
357\textbf{How widely used}: Seldom. The only time this affects existing C code is when the size of an enumerator is taken.
358Taking the size of an enumerator is not a common C coding practice.
359\end{quote}
360Hence, the values in a \CC enumeration can only be its enumerators (without a cast).
361While the storage size of an enumerator is up to the compiler, there is still an implicit cast to @int@.
362\begin{c++}
363enum E { A, B, C };
364E e = A;
365int i = A;   i = e;                                             $\C{// implicit casts to int}$
366\end{c++}
367\CC{11} added a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct}), so the enumerators are local to the enumeration and must be accessed using type qualification.
368\begin{c++}
369enum class E { A, B, C };
370E e = @E::@A;                                                   $\C{// qualified enumerator}$
371e = B;                                                                  $\C{// B not in scope}$
372\end{c++}
373\CC{20} supports unscoped access with a \lstinline[language=c++]{using enum} declaration.
374\begin{c++}
375enum class E { A, B, C };
376@using enum E;@
377E e = A;                                                                $\C{// direct access}$
378e = B;                                                                  $\C{// direct access}$
379\end{c++}
380\CC{11} added the ability to explicitly declare the underlying integral type for \lstinline[language=c++]{enum class}.
381\begin{c++}
382enum class RGB @: long@ { Red, Green, Blue };
383enum class rgb @: char@ { Red = 'r', Green = 'g', Blue = 'b' };
384enum class srgb @: signed char@ { Red = -1, Green = 0, Blue = 1 };
385\end{c++}
386There is no implicit conversion from the \lstinline[language=c++]{enum class} type and to its type.
387\begin{c++}
388rgb crgb = rgb::Red;
389char ch = rgb::Red;   ch = crgb;                $\C{// disallowed}$
390\end{c++}
391Finally, there is no mechanism to iterate through an enumeration nor use the enumeration type to declare an array dimension.
392
393
394\section{Go}
395
396\lstnewenvironment{Go}[1][]{% necessary
397\lstset{
398language=Go,
399escapechar=\$,                          % LaTeX escape in CFA code
400moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
401}% lstset
402\lstset{#1}% necessary
403}{}
404
405What Is an Enum in Golang?
406
407An enumerator, or enum, is a data type in Golang that consists of a set of named, constant values. While Golang doesn't support enums, it can be implemented using the identifier iota with constants.
408
409However, in Golang, they're implemented quite differently than most other programming languages. Golang doesn't support enums directly, however, we can implement it using iota and constants.
410
411In order to implement enums in Golang, let's first understand what iota is and how it is used.
412
413 
414What Is Iota in Golang?
415
416iota is an identifier that is used with constant and can simplify constant definitions that use auto-increment numbers. The iota keyword represents integer constant starting from zero.
417
418The iota keyword represents successive integer constants 0, 1, 2, \ldots.
419It resets to 0 whenever the word const appears in the source code and increments after each const specification.
420\begin{Go}
421package main
422
423import "fmt"
424
425const (
426        c0 = iota
427        c1 = iota
428        c2 = iota
429)
430func main() {
431        fmt.Println(c0, c1, c2) //Print : 0 1 2
432}
433\end{Go}
434You can avoid writing successive iota in front of every constant. This can be simplified as in the below code listing:
435\begin{Go}
436package main
437
438import "fmt"
439
440const (
441        c0 = iota
442        c1
443        c2
444)
445
446func main() {
447        fmt.Println(c0, c1, c2) //Print : 0 1 2
448}
449\end{Go}
450To start a list of constants at 1 instead of 0, you can use iota in an arithmetic expression.
451\begin{Go}
452package main
453
454import "fmt"
455
456const (
457        c0 = iota + 1
458        c1
459        c2
460)
461
462func main() {
463        fmt.Println(c0, c1, c2) // Print : 1 2 3
464}
465\end{Go}
466You can use the blank identifier to skip a value in a list of constants.
467\begin{Go}
468package main
469
470import "fmt"
471
472const (
473        c1 = iota + 1
474        _
475        c3
476        c4
477)
478
479func main() {
480        fmt.Println(c1, c3, c4) // Print : 1 3 4
481}
482\end{Go}
483
484
485\section{Java}
486
487\section{Modula-3}
488
489\section{Rust}
490
491
492\section{Swift}
493
494\lstnewenvironment{swift}[1][]{% necessary
495\lstset{
496language=Swift,
497escapechar=\$,                          % LaTeX escape in CFA code
498moredelim=**[is][\color{red}]{@}{@},    % red highlighting @...@
499}% lstset
500\lstset{#1}% necessary
501}{}
502
503Model custom types that define a list of possible values.
504
505An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.
506
507If you are familiar with C, you will know that C enumerations assign related names to a set of integer values.
508Enumerations in Swift are much more flexible, and don't have to provide a value for each case of the enumeration.
509If a value (known as a raw value) is provided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type.
510
511Alternatively, enumeration cases can specify associated values of any type to be stored along with each different case value, much as unions or variants do in other languages.
512You can define a common set of related cases as part of one enumeration, each of which has a different set of values of appropriate types associated with it.
513
514Enumerations in Swift are first-class types in their own right.
515They adopt many features traditionally supported only by classes, such as computed properties to provide additional information about the enumeration's current value, and instance methods to provide functionality related to the values the enumeration represents.
516Enumerations can also define initializers to provide an initial case value;
517can be extended to expand their functionality beyond their original implementation; and can conform to protocols to provide standard functionality.
518
519For more about these capabilities, see Properties, Methods, Initialization, Extensions, and Protocols.
520
521\paragraph{Enumeration Syntax}
522
523You introduce enumerations with the @enum@ keyword and place their entire definition within a pair of braces:
524\begin{swift}
525enum SomeEnumeration {
526        // enumeration definition goes here
527}
528\end{swift}
529Here's an example for the four main points of a compass:
530\begin{swift}
531enum CompassPoint {
532        case north
533        case south
534        case east
535        case west
536}
537\end{swift}
538The values defined in an enumeration (such as @north@, @south@, @east@, and @west@) are its enumeration cases.
539You use the @case@ keyword to introduce new enumeration cases.
540
541Note:
542Swift enumeration cases don't have an integer value set by default, unlike languages like C and Objective-C.
543In the CompassPoint example above, @north@, @south@, @east@ and @west@ don't implicitly equal 0, 1, 2 and 3.
544Instead, the different enumeration cases are values in their own right, with an explicitly defined type of CompassPoint.
545
546Multiple cases can appear on a single line, separated by commas:
547\begin{swift}
548enum Planet {
549        case mercury, venus, earth, mars, jupiter, saturn, uranus, neptune
550}
551\end{swift}
552Each enumeration definition defines a new type.
553Like other types in Swift, their names (such as @CompassPoint@ and @Planet@) start with a capital letter.
554Give enumeration types singular rather than plural names, so that they read as self-evident:
555\begin{swift}
556var directionToHead = CompassPoint.west
557\end{swift}
558The type of @directionToHead@ is inferred when it's initialized with one of the possible values of @CompassPoint@.
559Once @directionToHead@ is declared as a @CompassPoint@, you can set it to a different @CompassPoint@ value using a shorter dot syntax:
560\begin{swift}
561directionToHead = .east
562\end{swift}
563The type of @directionToHead@ is already known, and so you can drop the type when setting its value.
564This makes for highly readable code when working with explicitly typed enumeration values.
565
566\paragraph{Matching Enumeration Values with a Switch Statement}
567
568You can match individual enumeration values with a switch statement:
569\begin{swift}
570directionToHead = .south
571switch directionToHead {
572case .north:
573        print("Lots of planets have a north")
574case .south:
575        print("Watch out for penguins")
576case .east:
577        print("Where the sun rises")
578case .west:
579        print("Where the skies are blue")
580}
581// Prints "Watch out for penguins"
582\end{swift}
583You can read this code as:
584\begin{quote}
585"Consider the value of directionToHead.
586In the case where it equals @.north@, print "Lots of planets have a north".
587In the case where it equals @.south@, print "Watch out for penguins"."
588
589...and so on.
590\end{quote}
591As described in Control Flow, a switch statement must be exhaustive when considering an enumeration's cases.
592If the case for @.west@ is omitted, this code doesn't compile, because it doesn't consider the complete list of @CompassPoint@ cases.
593Requiring exhaustiveness ensures that enumeration cases aren't accidentally omitted.
594
595When it isn't appropriate to provide a case for every enumeration case, you can provide a default case to cover any cases that aren't addressed explicitly:
596\begin{swift}
597let somePlanet = Planet.earth
598switch somePlanet {
599case .earth:
600        print("Mostly harmless")
601default:
602        print("Not a safe place for humans")
603}
604// Prints "Mostly harmless"
605\end{swift}
606
607\paragraph{Iterating over Enumeration Cases}
608
609For some enumerations, it's useful to have a collection of all of that enumeration's cases.
610You enable this by writing @CaseIterable@ after the enumeration's name.
611Swift exposes a collection of all the cases as an allCases property of the enumeration type.
612Here's an example:
613\begin{swift}
614enum Beverage: CaseIterable {
615        case coffee, tea, juice
616}
617let numberOfChoices = Beverage.allCases.count
618print("\(numberOfChoices) beverages available")
619// Prints "3 beverages available"
620\end{swift}
621In the example above, you write @Beverage.allCases@ to access a collection that contains all of the cases of the @Beverage@ enumeration.
622You can use @allCases@ like any other collection -- the collection's elements are instances of the enumeration type, so in this case they're Beverage values.
623The example above counts how many cases there are, and the example below uses a for-in loop to iterate over all the cases.
624\begin{swift}
625for beverage in Beverage.allCases {
626        print(beverage)
627}
628// coffee
629// tea
630// juice
631\end{swift}
632The syntax used in the examples above marks the enumeration as conforming to the @CaseIterable@ protocol.
633For information about protocols, see Protocols.
634
635\paragraph{Associated Values}
636The examples in the previous section show how the cases of an enumeration are a defined (and typed) value in their own right.
637You can set a constant or variable to Planet.earth, and check for this value later.
638However, it's sometimes useful to be able to store values of other types alongside these case values.
639This additional information is called an associated value, and it varies each time you use that case as a value in your code.
640
641You can define Swift enumerations to store associated values of any given type, and the value types can be different for each case of the enumeration if needed.
642Enumerations similar to these are known as discriminated unions, tagged unions, or variants in other programming languages.
643
644For example, suppose an inventory tracking system needs to track products by two different types of barcode.
645Some products are labeled with 1D barcodes in UPC format, which uses the numbers 0 to 9.
646Each barcode has a number system digit, followed by five manufacturer code digits and five product code digits.
647These are followed by a check digit to verify that the code has been scanned correctly:
648
649Other products are labeled with 2D barcodes in QR code format, which can use any ISO 8859-1 character and can encode a string up to 2,953 characters long:
650
651It's convenient for an inventory tracking system to store UPC barcodes as a tuple of four integers, and QR code barcodes as a string of any length.
652
653In Swift, an enumeration to define product barcodes of either type might look like this:
654\begin{swift}
655enum Barcode {
656        case upc(Int, Int, Int, Int)
657        case qrCode(String)
658}
659\end{swift}
660This can be read as:
661\begin{quote}
662"Define an enumeration type called Barcode, which can take either a value of upc with an associated value of type @(Int, Int, Int, Int)@, or a value of @qrCode@ with an associated value of type @String@."
663\end{quote}
664This definition doesn't provide any actual @Int@ or @String@ values -- it just defines the type of associated values that Barcode constants and variables can store when they're equal to @Barcode.upc@ or @Barcode.qrCode@.
665
666You can then create new barcodes using either type:
667\begin{swift}
668var productBarcode = Barcode.upc(8, 85909, 51226, 3)
669\end{swift}
670This example creates a new variable called @productBarcode@ and assigns it a value of @Barcode.upc@ with an associated tuple value of @(8, 85909, 51226, 3)@.
671
672You can assign the same product a different type of barcode:
673\begin{swift}
674productBarcode = .qrCode("ABCDEFGHIJKLMNOP")
675\end{swift}
676At this point, the original @Barcode.upc@ and its integer values are replaced by the new @Barcode.qrCode@ and its string value.
677Constants and variables of type Barcode can store either a @.upc@ or a @.qrCode@ (together with their associated values), but they can store only one of them at any given time.
678
679You can check the different barcode types using a switch statement, similar to the example in Matching Enumeration Values with a Switch Statement.
680This time, however, the associated values are extracted as part of the switch statement.
681You extract each associated value as a constant (with the let prefix) or a variable (with the var prefix) for use within the switch case's body:
682\begin{swift}
683switch productBarcode {
684case .upc(let numberSystem, let manufacturer, let product, let check):
685        print("UPC: \(numberSystem), \(manufacturer), \(product), \(check).")
686case .qrCode(let productCode):
687        print("QR code: \(productCode).")
688}
689// Prints "QR code: ABCDEFGHIJKLMNOP."
690\end{swift}
691If all of the associated values for an enumeration case are extracted as constants, or if all are extracted as variables, you can place a single let or var annotation before the case name, for brevity:
692\begin{swift}
693switch productBarcode {
694case let .upc(numberSystem, manufacturer, product, check):
695        print("UPC : \(numberSystem), \(manufacturer), \(product), \(check).")
696case let .qrCode(productCode):
697        print("QR code: \(productCode).")
698}
699// Prints "QR code: ABCDEFGHIJKLMNOP."
700\end{swift}
701
702\paragraph{Raw Values}
703
704The barcode example in Associated Values shows how cases of an enumeration can declare that they store associated values of different types.
705As an alternative to associated values, enumeration cases can come prepopulated with default values (called raw values), which are all of the same type.
706
707Here's an example that stores raw ASCII values alongside named enumeration cases:
708\begin{swift}
709enum ASCIIControlCharacter: Character {
710        case tab = "\t"
711        case lineFeed = "\n"
712        case carriageReturn = "\r"
713}
714\end{swift}
715Here, the raw values for an enumeration called ASCIIControlCharacter are defined to be of type Character, and are set to some of the more common ASCII control characters.
716Character values are described in Strings and Characters.
717
718Raw values can be strings, characters, or any of the integer or floating-point number types.
719Each raw value must be unique within its enumeration declaration.
720
721Note
722
723Raw values are not the same as associated values.
724Raw values are set to prepopulated values when you first define the enumeration in your code, like the three ASCII codes above.
725The raw value for a particular enumeration case is always the same.
726Associated values are set when you create a new constant or variable based on one of the enumeration's cases, and can be different each time you do so.
727Implicitly Assigned Raw Values
728
729When you're working with enumerations that store integer or string raw values, you don't have to explicitly assign a raw value for each case.
730When you don't, Swift automatically assigns the values for you.
731
732For example, when integers are used for raw values, the implicit value for each case is one more than the previous case.
733If the first case doesn't have a value set, its value is 0.
734
735The enumeration below is a refinement of the earlier Planet enumeration, with integer raw values to represent each planet's order from the sun:
736
737\begin{swift}
738enum Planet: Int {
739        case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
740}
741\end{swift}
742In the example above, Planet.mercury has an explicit raw value of 1, Planet.venus has an implicit raw value of 2, and so on.
743
744When strings are used for raw values, the implicit value for each case is the text of that case's name.
745
746The enumeration below is a refinement of the earlier CompassPoint enumeration, with string raw values to represent each direction's name:
747\begin{swift}
748enum CompassPoint: String {
749        case north, south, east, west
750}
751\end{swift}
752In the example above, CompassPoint.south has an implicit raw value of "south", and so on.
753
754You access the raw value of an enumeration case with its rawValue property:
755\begin{swift}
756let earthsOrder = Planet.earth.rawValue
757// earthsOrder is 3
758
759let sunsetDirection = CompassPoint.west.rawValue
760// sunsetDirection is "west"
761\end{swift}
762
763\paragraph{Initializing from a Raw Value}
764
765If you define an enumeration with a raw-value type, the enumeration automatically receives an initializer that takes a value of the raw value's type (as a parameter called rawValue) and returns either an enumeration case or nil.
766You can use this initializer to try to create a new instance of the enumeration.
767
768This example identifies Uranus from its raw value of 7:
769\begin{swift}
770let possiblePlanet = Planet(rawValue: 7)
771// possiblePlanet is of type Planet? and equals Planet.uranus
772\end{swift}
773Not all possible Int values will find a matching planet, however.
774Because of this, the raw value initializer always returns an optional enumeration case.
775In the example above, possiblePlanet is of type Planet?, or "optional Planet."
776Note
777
778The raw value initializer is a failable initializer, because not every raw value will return an enumeration case.
779For more information, see Failable Initializers.
780
781If you try to find a planet with a position of 11, the optional Planet value returned by the raw value initializer will be nil:
782\begin{swift}
783let positionToFind = 11
784if let somePlanet = Planet(rawValue: positionToFind) {
785        switch somePlanet {
786        case .earth:
787                print("Mostly harmless")
788        default:
789                print("Not a safe place for humans")
790        }
791} else {
792        print("There isn't a planet at position \(positionToFind)")
793}
794// Prints "There isn't a planet at position 11"
795\end{swift}
796This example uses optional binding to try to access a planet with a raw value of 11.
797The statement if let somePlanet = Planet(rawValue: 11) creates an optional Planet, and sets somePlanet to the value of that optional Planet if it can be retrieved.
798In this case, it isn't possible to retrieve a planet with a position of 11, and so the else branch is executed instead.
799
800\paragraph{Recursive Enumerations}
801
802A recursive enumeration is an enumeration that has another instance of the enumeration as the associated value for one or more of the enumeration cases.
803You indicate that an enumeration case is recursive by writing indirect before it, which tells the compiler to insert the necessary layer of indirection.
804
805For example, here is an enumeration that stores simple arithmetic expressions:
806\begin{swift}
807enum ArithmeticExpression {
808        case number(Int)
809        indirect case addition(ArithmeticExpression, ArithmeticExpression)
810        indirect case multiplication(ArithmeticExpression, ArithmeticExpression)
811}
812\end{swift}
813You can also write indirect before the beginning of the enumeration to enable indirection for all of the enumeration's cases that have an associated value:
814\begin{swift}
815indirect enum ArithmeticExpression {
816        case number(Int)
817        case addition(ArithmeticExpression, ArithmeticExpression)
818        case multiplication(ArithmeticExpression, ArithmeticExpression)
819}
820\end{swift}
821This enumeration can store three kinds of arithmetic expressions: a plain number, the addition of two expressions, and the multiplication of two expressions.
822The addition and multiplication cases have associated values that are also arithmetic expressions -- these associated values make it possible to nest expressions.
823For example, the expression (5 + 4) * 2 has a number on the right-hand side of the multiplication and another expression on the left-hand side of the multiplication.
824Because the data is nested, the enumeration used to store the data also needs to support nesting -- this means the enumeration needs to be recursive.
825The code below shows the ArithmeticExpression recursive enumeration being created for (5 + 4) * 2:
826\begin{swift}
827let five = ArithmeticExpression.number(5)
828let four = ArithmeticExpression.number(4)
829let sum = ArithmeticExpression.addition(five, four)
830let product = ArithmeticExpression.multiplication(sum, ArithmeticExpression.number(2))
831\end{swift}
832A recursive function is a straightforward way to work with data that has a recursive structure.
833For example, here's a function that evaluates an arithmetic expression:
834\begin{swift}
835func evaluate(_ expression: ArithmeticExpression) -> Int {
836        switch expression {
837        case let .number(value):
838                return value
839        case let .addition(left, right):
840                return evaluate(left) + evaluate(right)
841        case let .multiplication(left, right):
842                return evaluate(left) * evaluate(right)
843        }
844}
845
846print(evaluate(product))
847// Prints "18"
848\end{swift}
849This function evaluates a plain number by simply returning the associated value.
850It evaluates an addition or multiplication by evaluating the expression on the left-hand side, evaluating the expression on the right-hand side, and then adding them or multiplying them.
851
852
853\section{Python}
854
855\section{Algebraic Data Type}
Note: See TracBrowser for help on using the repository browser.