comp.lang.ada
 help / color / mirror / Atom feed
From: Richard D Riehle <laoXhai@ix.netcom.com>
Subject: Re: Elimination of "use" clauses
Date: 1999/07/20
Date: 1999-07-19T20:02:31-05:00	[thread overview]
Message-ID: <7n0hr7$180@dfw-ixnews21.ix.netcom.com> (raw)
In-Reply-To: FF2ECA.76@stuyts.nl

In article <FF2ECA.76@stuyts.nl>,
	jerry@jvdsys.stuyts.nl wrote:

>I am unfamiliar with the term 'opaque type', perhaps because I never looked
>at Modula-3. Can you explain what it means ?

I will answer this question with the warning that my examples
are simply a repetition of already well-known coding practices. 
Also, even though the Ada code will seem slightly longer than
the Modula-3, there are good reasons to prefer the Ada.  Even
so, the ease of creating opaque types in Modula-3 is an attractive
features of that language.

An "opaque type" is a type in which all the details of the type are
encapsulated in an implementation part of a module.  This is required
in Modula-3.  Ada allows us to promote some of the definition to the
private part of the specification.   Ada also allows us to defer the
details to the pacakge body. 

The opaque type is commonly used in Modula-3 where we would use a [limited]
private type in Ada.  

The Modula-3 equivalent of a package specification is now called 
the INTERFACE and equivalent of the package body is called
a MODULE.  Modula-3 does not have a private part in the INTERFACE.
Consider classic generic Stack example in  Modula-3,

GENERIC INTERFACE Stack (SomeElementType);
  TYPE Stack_Type <: REFANY;
  (* procedures for push and pop);
END Stack;

GENERIC MODULE Stack(SomeElementType);
 REVEAL Stack_Type = more details for the stack type
 implementation of push, pop, etc.
END Stack;

My appreciation to MODULA-3 experts who might notice some syntatic
error in my example.  The REVEAL command gives the full definition of
the type from the INTERFACE.
 
In Ada, a generic stack might be designed several ways. We first show the
common textbook example. 

generic
  type StackElementType is private;
  Max  : Positive := 100;
package Stack is
  type Stack_Type is limited private;
  -- declarations for push, pop, etc.
private
  type Stack_Array is array(Positive range <>) of StackElementType;
  type Stack is record
     Top : Natural := 0;
     Data : Stack_Array(1..Max);
  end record;
end Stack;
 
In this example, the private part contains all the information about 
the structure of the stack.  The second Ada example is designed as an 
"opaque type".  

generic
  type StackElementType is private;
  Max  : Positive := 100;
package Stack is
  type Stack_Type is limited private;
  -- declarations for push, pop, etc.
private
  type Stack_Data;
  type Stack is access all Stack_Data;  
end Stack;

where there is no information about the structure of the stack,
even in the private part.  The details of the stack are deferred
to the package body.  For example,

package body Stack is
  type Stack_Array is array(1..Max) of StackElementType;
  subtype Stack_Index is range 0..Max;
  type Stack_Item is record
    Top : Stack_Index := 0;
    Data : Stack_Array;
  end record;
  -- implement push, pop, etc. 
end Stack;

Richard Riehle
richard@adaworks.com
http://www.adaworks.com




  parent reply	other threads:[~1999-07-20  0:00 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-07-01  0:00 Elimination of "use" clauses Dr. Neil C. Audsley
1999-07-01  0:00 ` Joe Wisniewski
1999-07-01  0:00 ` czgrr
1999-07-01  0:00   ` Ted Dennison
1999-07-02  0:00     ` czgrr
1999-07-02  0:00       ` Ted Dennison
1999-07-01  0:00 ` Samuel T. Harris
1999-07-02  0:00 ` Robert Dewar
1999-07-02  0:00   ` Samuel T. Harris
1999-07-02  0:00     ` Robert Dewar
     [not found]       ` <7ltl2q$mog$1@nnrp1.deja.com>
1999-07-08  0:00         ` Michael F. Yoder
1999-07-09  0:00           ` Richard D Riehle
1999-07-09  0:00             ` Marin David Condic
1999-07-09  0:00             ` Michael F. Yoder
1999-07-09  0:00           ` Robert Dewar
1999-07-09  0:00             ` Dale Stanbrough
1999-07-12  0:00               ` Robert Dewar
1999-07-12  0:00                 ` Ted Dennison
1999-07-09  0:00             ` Michael F. Yoder
1999-07-14  0:00               ` Tucker Taft
1999-07-10  0:00             ` Simon Wright
1999-07-12  0:00               ` Robert Dewar
1999-07-08  0:00       ` R. Tim Coslet
1999-07-09  0:00         ` Robert Dewar
1999-07-09  0:00           ` tmoran
1999-07-02  0:00   ` Ed Falis
1999-07-03  0:00     ` Joe Wisniewski
1999-07-03  0:00       ` Ed Falis
1999-07-03  0:00       ` Keith Thompson
1999-07-13  0:00     ` Peter Amey
1999-07-02  0:00   ` Ted Dennison
1999-07-02  0:00     ` Stephen Leake
1999-07-02  0:00     ` Robert Dewar
     [not found]       ` <7ltus1$ah1@dfw-ixnews19.ix.netcom.com>
1999-07-13  0:00         ` Robert A Duff
1999-07-18  0:00           ` Richard D Riehle
1999-07-18  0:00             ` Dale Stanbrough
1999-07-20  0:00               ` David Kristola
1999-07-20  0:00               ` Richard D Riehle
1999-07-19  0:00                 ` Brian Rogoff
1999-07-20  0:00                   ` Robert Dewar
1999-07-20  0:00                     ` Brian Rogoff
1999-07-21  0:00                       ` Robert Dewar
1999-07-21  0:00                         ` Brian Rogoff
1999-07-22  0:00                           ` Robert Dewar
1999-07-22  0:00                             ` Brian Rogoff
1999-07-22  0:00                           ` Robert Dewar
1999-07-22  0:00                             ` Brian Rogoff
1999-07-21  0:00                       ` Ted Dennison
1999-07-21  0:00                         ` Robert A Duff
1999-07-21  0:00                         ` Robert Dewar
1999-07-21  0:00                     ` Robert A Duff
1999-07-21  0:00                       ` Michael F. Yoder
1999-07-21  0:00                         ` Robert A Duff
1999-07-23  0:00                 ` Tucker Taft
1999-08-03  0:00                   ` Richard D Riehle
1999-07-18  0:00             ` jerry
1999-07-19  0:00               ` Vladimir Olensky
1999-07-20  0:00               ` Richard D Riehle [this message]
1999-07-20  0:00                 ` Opaque Types (was Elimination of "use" clauses) David C. Hoos, Sr.
1999-07-20  0:00                 ` Elimination of "use" clauses jerry
1999-07-19  0:00             ` Ted Dennison
1999-07-19  0:00               ` Tucker Taft
1999-07-19  0:00                 ` Ted Dennison
1999-07-02  0:00     ` Ed Falis
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox