A useful, general OOP pattern

Emmanuel Briot has published an interesting extension to my usual guidelines (happily stolen from the Quality and Style Guide) on declaring tagged types.

Basically the pattern is (in my version):

subtype Parent is Parent_Type_Package.Instance;
type Instance is new Parent with private;
subtype Class is Instance'Class;
type Reference is access all Class; --  Optional, only if you really need access types

What do you think of it?

And do you prefer Emmanuel’s naming style or mine?

Overridable Class Attributes in Ada 2012

One of the nicest features of the AdaCore website is the Ada Gem of the Week. Currently sitting at 117 gems, this place is a regular treasure-trove for Ada programming.

Today’s gem is written by Emmanuel Briot and it deals with some of the new Ada 2012 features, specifically in relation to object oriented Ada programming. it’s called Gem #117: Design Pattern: Overridable Class Attributes in Ada 2012.

Most object-oriented programming languages provide a facility for declaring variables that are shared by all objects of a given class. In C++, these are called “static members” (and use the “static” keyword), and similarly Python has the notion of “class attributes”.

Let’s consider an example where this is useful. For instance, let’s say we want to define the notion of a block of text that is generated by expanding a template (perhaps after we replace some parameters in that template, as can be done with AWS’s templates parser, for instance). Once we have computed those parameters, we might want to generate multiple outputs (for instance HTML and CSV). Only the template needs to change, not the computation of the parameters.

I can’t say I’m a huge expert on object oriented Ada programming, but this little snippet actually made me hungry for playing around with some of the new Ada 2012 features, such as expression functions, where you can do stuff like this:

pragma Ada_2012;
 
type Text_Block is tagged null record;
function Template (Self : Text_Block) return String
   is ("filename.txt");
function Render (Self : Text_Block) return String;

Instead of the old more clunky method:

type Text_Block is tagged null record;
function Template (Self : Text_Block) return String;
function Render (Self : Text_Block) return String;
 
function Template (Self : Text_Block) return String is
   pragma Unreferenced (Self);
begin
   return "file_name.txt";
end Template;

Very nice indeed! The new Ada 2012 syntax allows for a much more simple and readable setup of the Text_Block type. But don’t take my word for it, go read the Gem instead. It is definitely worth it.

Dynamic Plug-in Loading with Ada

If you want/need to learn how to make use of dynamic plug-ins, then look no further! In the excellent article Dynamic Plug-in Loading with Ada (PDF) authors Cyrille Comar and Pat Rogers, both of AdaCore fame, dive into the world of dynamic plug-ins, object oriented programming and a crude simulation of instruments on an automobile dashboard.

Maintenance of high-availability systems (e.g., servers) requires the ability to modify, enhance, or correct parts of the application without having to stop and re-link the entire system. This capability is relatively straight-forward with interpreted languages or virtual-machine based languages such as Java, in which new code is loaded upon demand. In languages typically
implemented with static executable images this capability can be offered though dynamically loaded/linked libraries (“DLLs”). However, in practice it is impractical to make full use of this
capability because the protocol for invoking subprograms in a DLL is very low-level and unsafe. In the case of Ada, global coherency requirements and elaboration ordering constraints add an
additional degree of complexity over less strict/safe languages.
Object-oriented programming makes this approach practical by using dynamic dispatching to invoke dynamically loaded functions with a more robust, high-level protocol. In an OO paradigm, a “plug-in” contains new classes that enrich the class set of the original application. Calls to subprograms in the shared library (plug-in) are done implicitly through dynamic dispatching which is much simpler, transparent to the programmer, type-safe, and more robust. This application note shows how a statically-typed, statically-built, object-oriented language such
as Ada can make full use of the notion of dynamic plug-ins á la Java without relying on a comparatively inefficient virtual machine. We build an extensible application and illustrate
adding new functionality at run-time, without first stopping execution, using plug-ins.

It’s a very informative read, with lots of source code coupled with well-written explanations. Definitely worth your time, if you’re interested in Ada, OO and dynamic modules/plug-ins.

If you don’t care for PDF’s, there’s a somewhat abbreviated version of the article available at Dr.Dobb’s.

High-integrity object-oriented programming with Ada – Part 3

A few weeks ago I wrote about the High-integrity object-oriented programming with Ada articles by Benjamin Brosgol. While I was away on vacation, part 3 was made available:

Be sure to check out these articles if you’re interested in how Ada does OOP.

High-integrity object-oriented programming with Ada

Written by Benjamin Brosgol from AdaCore, this article series digs deep into the bowels of Ada and its OOP features, specifically into high-integrity systems. It is a very interesting read, with lots of exact information. Clearly Benjamin is an experienced and skilled engineer.

It’s a three part article, of which two parts are currently available.

Part 1 of this three-part article reviews the basics of object-oriented programming and summarizes the challenges it presents for high-integrity programming. Part 2 will provide a primer on the Ada programming language, and Part 3 will detail the tools Ada offers to help developers meet the OOP challenges.

If you are in any way interested in Ada and OOP, then these articles can serve as a good place to start.

I will of course link to part 3 of the article series, as soon as it’s made available.