Enumerated Types
Concept
Enumerated Types Create New Literals!
- We have created new literals in the language!
- What is a literal in a language?
- Something that can be represented directly
- Examples: 3 vs 2 + 1, "Hi Mom", true vs not false
Enumerated Types - Questions
- Questions:
- What operations are available
- Case sensitive?
- Can two types mix names
- Can variables and types mix names
- Rules for keeping sets of names distinct
- Motivation and approach of other languages
Example with an Enumerated Type
Prettified: enum1.adb.html
Example:
-- Illustrates:
-- declaring types and variables
-- relational operators and in
with Ada.Text_io; use Ada.Text_io;
procedure enum1 is
type Day is (Sunday, Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday);
today: Day;
begin
today := Thursday;
if today = Friday then
put("We made it!");
end if;
for d in Sunday .. Saturday loop
if d in Monday .. Friday then
put("It's a work day.");
if d = Monday then
put("The first of the week.");
end if;
else
put("It's the weekend!");
end if;
end loop;
end enum1;
Enumerated Types are Strongly Typed
- Types that are distinct can't be mixed!
with Ada.Text_io; use Ada.Text_io;
procedure enum1a is
type Color is (Red, Blue, Green);
type Day is (Sunday, Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday);
c: Color;
d: Day;
n: Natural := 1;
begin
c := Red;
d := Friday;
c := Friday; -- Compile error
c := d; -- Compile error
c := 1; -- Compile error
d := 1; -- Compile error
n := c; -- Compile error
c := n; -- Compile error
if d = Blue then -- Compile error
put("It's Friday!");
end if;
end enum1a;
I/O of Enumerated Types
- Because of strong typing
we need get and put for values of a new type
- New i/o packages must be created for each new type!
with Ada.Text_io; use Ada.Text_IO;
procedure enum2 is
type Color is (Red, Blue, Green);
type Day is (Sunday, Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday);
-- Declare 2 new I/O packages
package Color_IO is new Ada.Text_IO.Enumeration_IO(Color);
package Day_IO is new Enumeration_IO(Day);
-- Use the new packages
use Color_IO;
use Day_IO;
c: Color;
d: Day;
begin
c := Red;
d := Monday;
put(c);
new_line;
put(d);
end enum2;
SAMPLE RUN:
RED
MONDAY
Enumeration_IO is known as a generic package
- A type as a parameter
- Java 1.5 introduces parameterized types, which are similar
- We will study these in detail later
Color_IO.Get is also provided
A slightly more complex example
(enum2a.adb)
[and prettified]
that inputs enumerated type values and strings from the file
this data file
Important: Don't forget that your input file needs to be
created using the same operating system that your program is
compiled for! (For example, don't create your file using PFE on a
PC and then compile using SSH, which creates an executable for a
SPARC.)
More Operations: Attributes of Enumerated Types
- Example 1
- The usual: Range, First, Last
- Are plus, minus, etc available No, but these are:
- Sequence Functions: Succ, Pred
- Example 2
- Functions to convert to/from Naturals: Pos, Val
- Functions to convert to/from Strings: Image, Value
- Relops
- Example 3
Ambiguous Values
- What happens when the sets are not distinct
- Example: with Color Blue and Mood Blue?
- Example
Enumerated Type Values and Variables, etc
Subtypes (ie Subranges) of Enumeration
subtype Weekday is range Monday .. Friday;
- Example
Types Boolean and Character
- The following are defined to be in package Ada.Standard
(which is automatically available via the compiler rather than via a
library)
type Boolean is (True, False);
type Character is (..., 'A', 'B', ..., 'a', 'b', ...);
Motivation
- Write program at level of problem rather than level of machine
- More readable code
- Better type checking to catch more errors
void setDate(int day, month, year){...}
...
setDate(31, 9, 2003); // September 31
...
// A confused programmer might have coded this but meant September 8
setDate(9, 8, 2003); // August 9, 2003
// The compiler would not detect the error
- If there were a type for Months, then coding
setDate(September, 8, 2003) instead of setDate(8, September, 2003) would
be detected as an error
Enumerated Types in Other Languages
enum WeekDay { Monday, Tuesday, Wednesday, Thursday, Friday }
WeekDay today = Weekday.Monday;
Java enums are implemented using a parameterized class (ie generic)
called Enum
Java enums are objects, not primitive values.