Section 11.2 - Generic Formal Parameters

We will only look a little bit at generics, just enough so you'll understand what they can do. However, it's difficult to read and use generics if you don't understand what can be used in a generic formal parameter (i.e. the stuff right after the keyword "generic"). Here are the things that can be included in a generic formal parameter list:

Here's an example of a formal_object_declaration:

   Maximum_Size : Integer;

Here's the syntax for defining a value or variable as a formal object in BNF format:

  formal_object_declaration ::= identifier_list ":" [ "in" | "in out" ]
                                type_name [ ":=" default_expression ] ";"

We've already seen an example of a formal type declaration. Formal types specify the name of a type and what ``kind of type'' is permitted. A formal type declaration specifies the "minimum" or "worst case" kind of type that is required. The most minimal type in Ada is called a "limited private" type. This is the "worst case" because the keyword "private" means that you may not know anything about how it's implemented, and "limited" means that there might not be assignment or equality operations defined for it.

A formal type declaration has the following syntax (this is actually highly simplified; many more things are permitted):

  formal_type_declaration ::= "type" defining_identifier "is"
                              formal_type_definition ";"

  formal_type_definition ::= ["tagged"] ["limited"] "private" | "(<>)"

Let's look at some examples, with their meaning written beside them:

  type Item is limited private;  -- Item can be any type.
  type Item is private;          -- Item can be any type that has assignment
                                 -- (:=) and equal-to (=) operation.
  type Item is tagged limited private; -- Item can be any tagged type.
  type Item is tagged private;   -- Item can be any tagged type with :=.
  type Item is (<>);             -- Item can be any discrete type, including
                                 -- Integer and Boolean.

In the next section we'll look at an example that should make these things clearer.


If you want to create a generic with a formal type named Unit that could be any type at all, how would you declare it?

  1. type Unit is limited private;
  2. type Unit is private;
  3. type Unit is (<>);

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 11 outline

David A. Wheeler (

The master copy of this file is at "".