Enumerated Types



Concept



Enumerated Types Create New Literals!



Enumerated Types - Questions



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

     
    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



    More Operations: Attributes of Enumerated Types



    Ambiguous Values



    Enumerated Type Values and Variables, etc



    Enumerated Types as Array Indices



    Subtypes (ie Subranges) of Enumeration



    Types Boolean and Character

     
         type Boolean is (True, False);
    
         type Character is (..., 'A', 'B', ..., 'a', 'b', ...);
      



    Motivation



    Enumerated Types in Other Languages