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
Enumerated Types as Array Indices
- An enumerated type range can be an array index
type Day is (Sunday, Monday, Tuesday,
Wednesday, Thursday, Friday, Saturday);
type HoursArray is array(Monday .. Saturday) of Float;
hoursWorked: HoursArray := (others => 0.0);
...
for d in Monday .. Friday loop
totalHours := totalHours + hoursWorked(d);
end loop
totalHours := totalHours + 1.5 * (hoursWorked(Saturday));
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
- Example: possible to write
if roomcolor = blue
rather thanif roomcolor = 3
- Result: code is easier to write and more readable
- Better type checking to catch more errors
roomColor *= 3;
void setDate(int day, int month, int year){...}
...
// How to call setDate for September 8, 2003?
setDate(9, 8, 2003); // Which is correct?
setDate(8, 9, 2003);
// The compiler would not detect the error
- If there were a type for Months, the compiler would flag
one of these as invalid:
setDate(September, 8, 2003);
setDate(8, September, 2003);
Enumerated Types in Other Languages
- Java uses
final static int for constant values
- This does not provide type checking
- Example: Class Calendar has method
void set(int field, int value)
for setting a specific field in a calendar
myCalendar.set(DAY_OF_WEEK, SUNDAY); // set day of week of myCalendar to 1
myCalendar.set(MONTH, JANUARY ); // set month of myCalendar to 0
For the parameter field, class Calendar has int
constants
for which field to set and for field values:
class Calendar{
public static final int YEAR=1, MONTH=2, ..., DAY_OF_WEEK=7, ...;
public static final int SUNDAY=1, MONDAY=2, ..., SATURDAY=7;
public static final int JANUARY=0, FEBRUARY=1, ..., DECEMBER=11;
Error checking:
myCalendar.set(SUNDAY, DAY_OF_WEEK); // Which is correct?
myCalendar.set(DAY_OF_WEEK, SUNDAY); // Can compiler catch error?
Similar problems with C, C++
- C has enums which are really are exactly like ints, thus no type checking
- C++ has enums which give some type checking
Java 1.5 added enums which are similar to Ada enumerated types
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.