Gem #1: Limited Types in Ada 2005 — Limited Aggregates

by Bob Duff—AdaCore

Let's get started…

One of my favorite features of Ada is the “full coverage rules” for aggregates. For example, suppose we have a record type:

   <b>type</b> Person <b>is</b>
      <b>record</b>
         Name : Unbounded_String;
         Age : Years;
      <b>end</b> <b>record</b>;

We can create an object of the type using an aggregate:

   X : <b>constant</b> Person :=
      (Name => To_Unbounded_String (”John Doe”),
       Age => 25);

The full coverage rules say that every component of Person must be accounted for in the aggregate. If we later modify type Person by adding a component:

   <b>type</b> Person <b>is</b>
      <b>record</b>
         Name : Unbounded_String;
         Age : Natural;
         Shoe_Size : Positive;
      <b>end</b> <b>record</b>;

and we forget to modify X accordingly, the compiler will remind us. Case statements also have full coverage rules, which serve a similar purpose.

Of course, we can defeat the full coverage rules by using “others” (usually for array aggregates and case statements, but occasionally useful for record aggregates):

   X : <b>constant</b> Person :=
      (Name => To_Unbounded_String (”John Doe”),
       <b>others</b> => 25);

According to the Ada RM “others” here means precisely the same thing as “Age | Shoe_Size”. But that's wrong: what “others” really means is “all the other components, including the ones we might add next week or next year”. That means you shouldn't use “others” unless you're pretty sure it should apply to all the cases that haven't been invented yet.

So far, this is old news — the full coverage rules have been aiding maintenance since Ada 83. So what does this have to do with Ada 2005?

Suppose we have a limited type:

   <b>type</b> Limited_Person <b>is</b> <b>limited</b>
      <b>record</b>
         Self : Limited_Person_Access := Limited_Person'Unchecked_Access;
         Name : Unbounded_String;
         Age : Natural;
         Shoe_Size : Positive;
      <b>end</b> <b>record</b>;

This type has a self-reference; it doesn't make sense to copy objects, because Self would end up pointing to the wrong place. Therefore, we would like to make the type limited, to prevent programmers from accidentally making copies. After all, the type is probably private, so the client programmer might not be aware of the problem. We could also solve that problem with controlled types, but controlled types are expensive, and add unnecessary complexity if not needed.

In Ada 95, aggregates were illegal for limited types. Therefore, we would be faced with a difficult choice: Make the type limited, and initialize it like this:

   X : Limited_Person;
   X.Name := To_Unbounded_String ("John Doe");
   X.Age := 25;

which has the maintenance problem the full coverage rules are supposed to prevent. Or, make the type nonlimited, and gain the benefits of aggregates, but lose the ability to prevent copies.

In Ada 2005, an aggregate is allowed to be limited; we can say:

   X : <b>aliased</b> Limited_Person :=
      (Self => <b>null</b>, <i>– Wrong!
</i>
       Name => To_Unbounded_String (”John Doe”),
       Age => 25,
       Shoe_Size => 10);
   X.Self := X'<b>Access</b>;

We'll see what to do about that “Self => null” in a future gem.

One very important requirement should be noted: the implementation is required to build the value of X “in place”; it cannot construct the aggregate in a temporary variable and then copy it into X, because that would violate the whole point of limited objects — you can't copy them


Last Updated: 11/6/2017
Posted on: 5/14/2007