TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Forum View

Use Monospaced Font
Show HTML Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Matthew Heaney <[log in to unmask]>
Reply To:
Matthew Heaney <[log in to unmask]>
Date:
Fri, 5 Feb 1999 21:44:58 -0800
Content-Type:
text/plain
Parts/Attachments:
text/plain (80 lines)
Samuel Mize <[log in to unmask]> writes:

> - if the type is indefinite, you can't declare an object of that
>   type.

This is not correct.  An indefinite type requires that the object be
initialized, either by copying another object, or invoking a constructor
function:

 package P is

    type T (<>) is private;

    function To_T (I : Integer) return T;

    ...
 end P;


   procedure Op (O : T) is
     Copy_Of_O : T := O;
   begin

or

   declare
      O : T := To_T (42);
   begin

So it is true that (variable or constant) objects of the type can be
declared, outside the package.


>   Of course, the full declaration makes it definite inside
>   the private parts of the package.  But you can't have an object
>   of that type outside the package.  This may be a problem or a
>   feature, depending on exactly what you're trying to do.

You may be confusing this with indefinite types that are also limited.
In that case, what you say is correct.

You can't declare an object of the type, because assignment isn't
available for the type, and so you can't make a copy or call an
initializer.

This is actually a feature of the abstraction, because it allows the
package declaring the type to control creation of all instances of the
type.

For example, in one of my recent posts to the patterns list at the ACM, I
showed how to use this technique to force clients to call a constructor
that returns a pointer to the limited, indefinite type.

Here's an excerpt:

The other change I made was to implement the types using memory
management techniques I described in my post about the Interpreter
pattern.  The type hierarchy is now limited and indefinite:

   type Root_Equipment (<>) is abstract tagged limited private;

This means clients can't create an Equipment instance without calling a
constructor explicitly.  Each type therefore provides an allocator
(deallocator) to create (destroy) instances of the type, as in

   function New_Hard_Disk
     return Hard_Disk_Access;

   procedure Free
     (Disk : in out Hard_Disk_Access);


The only way to create a hard disk (say) is to call the hard disk
constructor New_Hard_Disk.  Thus, the package controls how instances of
the type are created....

Here's the URL for the ACM patterns archive:

<http://www.acm.org/archives/patterns.html>

ATOM RSS1 RSS2