comp.lang.ada
 help / color / mirror / Atom feed
From: stt@henning.camb.inmet.com (Tucker Taft)
Subject: Re: access to subprogram disciminants
Date: 1996/04/25
Date: 1996-04-25T00:00:00+00:00	[thread overview]
Message-ID: <DqF8FG.BLG.0.-s@inmet.camb.inmet.com> (raw)
In-Reply-To: 4lm5t3$bqb@news2.delphi.com

tmoran@bix.com wrote:

: ...
: >: 2) Other than generics or discriminants of records (or tasks),
: >: what other ways are there of creating parameterized objects in Ada?
: >
: >Again, I am not exactly sure what you mean by "parameterized"
: >objects.  It might be helpful to understand the ultimate problem

: I have existing Ada 83 Windows code using generics in the form:

:    procedure main_window_procedure(Message, ...

:    package main_window is new Windows.frame_windows(
:      width  =>200,
:      height =>200,
:      title  =>"Cars and Drivers",
:      wndproc=>main_window_procedure);

:    car_list:main_window.listboxes;
:    driver_name_dialog:main_window.dialog_boxes;

:    begin

:    which_car:=main_window.open(car_list);

: etc.

: I think of 'main_window' as an object, parameterized by width, height,
: title, wndproc, etc.  Though generics have several nice characteristics,
: I can't easily make an array of them, for instance, so, now that
: 'access to subprogram' and 'access all' are available in Ada 95, it may
: be reasonable to have instead

:    type frame_windows(width, height: integer;
:                       title: string_ptr;
:                       wndproc: wndproc_ptr) is ...

:    main_window:aliased frame_windows(
:      width  =>200,
:      height =>200,
:      title  =>title_string_ptr,
:      wndproc=>main_window_procedure'access);

:    car_list:Windows.listboxes(main_window'access);
:    driver_name_dialog:Windows.dialog_boxes(main_window'access);

:    begin

:    which_car:=Windows.open(car_list);

: (Pardon typos and elisions.  This is typed for posting and is intended
: to show the idea, not to be running code.)

: That is what I mean by creating a parameterized object.  Unless I'm
: missing something, or some clever way of using record tags, Ada 95
: provides generics, records with discriminants, and tasks with
: discriminants, as ways of declaring such things.  (Discounting
: highly limited things like the size of an array as a 'parameter'
: of the array)

I think you are perhaps being misled by the RM's use of the 
term "parameterized" in association with type discriminants.  

What you are trying to accomplish can be done using normal record 
*components*, initialized as a result of calling a function
or procedure.  In other OOP's, such a function would often be called
a "constructor," but in Ada, you can just use normal functions or
procedures as "constructors."  

Don't try to make all of these "parameters" into discriminants.  
Just declare them as components of the data type.  Then, a user would
declare a simple "Frame_Window" or whatever, and initialize it from the
result of a function call, or by a subsequent procedure call.
The parameters to this initializing function/procedure are the
"parameters" you are probably interested in.  

This approach is much more flexible, as you can have multiple
constructor subprograms, taking different combinations of parameters.
If you are doing a GUI, chances are that you will be using access
types for representing these GUI elements, so it might be more
natural and efficient to make your constructor "functions"
return an access value, rather than a record object.
An additional advantage is that you can make the entire type
private, and choose to represent the information in various
ways.  If you make all the "parameters" into discriminants, then
they are always visible to all clients, and you are forced to represent
them explicitly, rather than perhaps implicitly in some other
part of the data type.

-Tucker Taft   stt@inmet.com   http://www.inmet.com/~stt/
Intermetrics, Inc.  Cambridge, MA  USA




  reply	other threads:[~1996-04-25  0:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-04-24  0:00 access to subprogram disciminants tmoran
1996-04-25  0:00 ` Tucker Taft [this message]
  -- strict thread matches above, loose matches on Subject: below --
1996-04-27  0:00 tmoran
1996-04-26  0:00 tmoran
1996-04-26  0:00 ` Tucker Taft
1996-04-26  0:00 ` Robert I. Eachus
1996-04-23  0:00 tmoran
1996-04-24  0:00 ` Tucker Taft
replies disabled

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