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:
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 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:
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,
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.