This is part of a series on programming in the Ada language, a mission critical programming language that I am attempting to learn and become proficient in.

When I first looked at the Ada language, one thing I found to be very attractive was the type system — unlike some other languages, it just “made sense” to me.

There are actually a lot of different kinds of types in Ada. The generic one that first caught my attention is the enumeration. Enums exist in C/C++ as well (and perhaps many other language), but an enum declaration in C hardly tells you what it means. In C, an enum would be declared like this:

enum Day_of_Week {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday
};

I have never understood enums in C particularly well despite several years of experience in the language. As a result, the most I have done with them is to use them as an equivalent of a series of constants like:

#define Sunday 0
#define Monday 1
/* ... */
#define Saturday 6

I’m sure that C is more powerful than that, but the more advanced features, based on a quick Google search, require memorizing a lot of extra syntax. One reason I chose C back in 2009 is its simple syntax — at least for the simple tasks I needed to do. C++ had ugly <brackets> and Fortran was…well, to say the least, unreadable. This was, naturally, several years before I even heard of Ada so it was not an option — which meant C was the most understandable language of its type I knew of. And that is how I became a C programmer. Then in came the features like enums and…

Well, enough of that — it’s time to return to the language that solves all (or at least a few) problems. Enumerations in Ada are MUCH more understandable. Compare the above to:

type Day_of_Week is (
   Sunday,
   Monday,
   Tuesday,
   Wednesday,
   Thursday,
   Friday,
   Saturday
);

Now THAT just makes sense, regardless of whether you know any Ada or not! Sunday, Monday, …, Saturday are all of the type “Day_of_Week” — no confusing “enum” keyword or resemblance to a function call. With this, a procedure or function can accept an argument of type “Day_of_Week” and be passed a Sunday, …, Saturday.

Perhaps more interesting is the ability to have subtypes which include just a portion of the main enumerated type. For example,

subtype Workday is Day_of_Week range Monday .. Saturday;

Now there’s something that should be very useful — especially with catching errors. I am a big fan of organization in code — mainly because if the code isn’t organized in a standardized and strict manner, I quickly lose track of which code does what. And when that happens, it’s quite easy to screw up. Organizing it this way also lets the compiler double check your work to make sure you organized correctly — because if you organized wrong, you probably made a mistake somewhere that wouldn’t be caught in a language like C.

I was originally planning on writing about the entire type system in this post. Those of you who are Ada programmers probably laughed just now at that statement. A quick look at the features of the type system show that one can probably write a whole book on it and this post would have ended up being upwards of 20 pages long. So, to keep things short and understandable, I’ll break it up into 20 posts instead.

So, next up in this sub-series on Ada types are records — the equivalent of structures in C.

Tagged with →  
Share →

2 Responses to Ada: Enumeration Types

  1. Joey says:

    Enumerations in Ada are, IMO, vastly superior to those of C & C++. The reason for this is the attributes that are accessible simply from declaring the type itself.

    Consider the example you gave, Day_of_Week, which lists those days’s names; that mere declaration (and a variable, Day, declared as follows: “Day : Day_of_Week:= Saturday;”) gives you access to:

    TYPE_NAME’Image( VARIABLE ) which returns the enumeration’s text as an uppercased string.
    EX: TYPE_NAME’Image( Day ) = “SATURDAY” would be true.

    TYPE_NAME’First and TYPE_NAME’Last, which return the first and last values in the enumeration list.

    The TYPE_NAME’Range attribute; essentially the same as “TYPE_NAME’First .. TYPE_NAME’Last”.
    EX: “For Index in Day_of_Week’Range” will iterate through all possible values of the enumeration; it never needs to be updated when, say, you need to add ‘Bobserday’ after Saturday.

    There’s also the ‘Val, ‘Value, ‘Pos attributes which, respectively:
    – Takes an integer denoting the position in the list and returns that enumeration.
    – Takes a String and returns the corresponding value; EX: TYPE_NAME’Image( “Saturday” ) returns Saturday. (That is this function is the inverse of ‘Image.)
    – Takes a variable of TYPE_NAME and returns its value’s position in the enumeration list. (It is the inverse function of ‘Val)

    There’s a few more, but these are the basic ones.
    If you will the Type-system of Ada is its leitmotiv, to borrow from music; if you understand defining types and subtypes there is a huge amount of effort you can save using Ada. One of my favorites, because its so readily grasped and intuitively useful, is the ability to exclude null from pointers (as of Ada 2005); you can say:
    Type My_Type is private; — Or whatever you need.
    Type My_Type_Access is Access All My_Type;
    SubType My_Type_Handle is Not Null My_Type_Access;
    and in the body of Procedure P( Param : My_TypeAccess ); you don’t need to check Param for Null because it’s already being done for you by the parameter. It’s equivalent, in some sense, to not allowing zero to be passed to division as a parameter [for the divisor] in the first place.

  2. Brian Drummond says:

    Nice post about learning Ada after C.

    You will come to like enumerations a lot more.
    Use them instead of integer subtypes for indexing arrays, or loops. Using your example,

    Activity : array (Day_Of_Week) of Activities := (Saturday => Party, Sunday => Sleep, others => Work);

    for i in Day_Of_Week loop
    Do(Activity(i));
    end loop;

    you can write very clean and clear programs, and you won’t get the array bounds wrong (iterating over C enums is not so nice!). Later you will find it easy to refactor and update, too…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>