type Color is (Red, Blue, Green); roomColor: Color := Blue;
-- Illustrates: -- declaring variables, assignment, relational operators, in, ranges 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 < Wednesday then put("Just started the week!."); end if; else put("It's the weekend!"); end if; end loop; end enum1;
type Color is (Red, Blue, Green); c: Color := red; begin put(c);
put
would be needed here? Color
is newly created, no put
for
it can already exist Color
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
type Color is (red, blue, green);
the valuesred, blue, green
are associated with values 0, 1, 2.
Enum_Rep
and Enum_Val
Convert between the literal and its enumeration representation procedure try_enum_rep is type Color is (red, blue, green); for Color use (red => 2, blue => 4, green => 16); c: Color := red; begin put(Color'enum_rep(c)); -- 2 c := blue; put(Color'enum_rep(c)); -- 4 c := green; put(c'enum_rep); -- 16 GNAT shorthand c := Color'enum_val(4); -- Inverse: c=Color'enum_val(c'enum_rep) put(c'img); -- GREEN end try_enum_rep;
red, blue, green
and 0, 1, 2, respectively red, blue, green
and 2, 4, 16, respectively 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;
type Color is (Red, Blue); Red: Integer; Blue: Color;
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));
subtype Weekday is range Monday .. Friday;
type Boolean is (True, False); type Character is (..., 'A', 'B', ..., 'a', 'b', ...);
with Standard
not allowed final static int
for constant values that act as
enumeration valuesvoid 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
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;
myCalendar.set(MONTH, 1); // January, or February? myCalendar.set(SUNDAY, DAY_OF_WEEK); // Which is correct? myCalendar.set(DAY_OF_WEEK, SUNDAY); // Can compiler catch error? myCalendar.set(MONTH, MONDAY); // Can error even be caught at runtime?
typedef enum {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY} week_days; week_days today = FRIDAY, tomorrow, dayAfter; tomorrow = today + 1; dayAfter = today * 2; int someInt = tomorrow; printf("%d %d %d %d\n", today, tomorrow, dayAfter, someInt); // Output: 5 6 10 6
tomorrow = today + 1;
gives an error, but ... int someInt = tomorrow ;
is allowed enum WeekDay { Monday, Tuesday, Wednesday, Thursday, Friday } WeekDay today = Weekday.Monday;
Enum
if roomcolor == blue
rather than if roomcolor == 3
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
setDate(September, 8, 2003); setDate(8, September, 2003);