comp.lang.ada
 help / color / mirror / Atom feed
From: "Samuel A. Mize" <smize@magellan.bgm.link.com>
Subject: Re: Syntax for tagged record types and class types
Date: 1997/05/22
Date: 1997-05-22T00:00:00+00:00	[thread overview]
Message-ID: <3384A4EA.167E@magellan.bgm.link.com> (raw)
In-Reply-To: 33830D58.6D8F@elca-matrix.ch


Mats Weber wrote:
> 
> Matthew Heaney wrote:
> 
> > I'm not dogmatic about the "pure" object-oriented syntax, ie
> >
> > declare
> >    O : T;
> > begin
> >    O.Op (A);
> > end;
> >
> > though I do think it's pretty hip.
> 
> So do I. And if tasks and protected types provide that kind of notation,
> why should this not also be true for other constructs ?
> 
> > What would be cool is to have a selector abstraction, to give you that for
> > free, ie
> >
> > type Stack is ...;
> >
> > selector Top (S : Stack) return Item;
> >
> > So that I could do this:
> >
> > declare
> >    The_Stack : Stack;
> > begin
> >    The_Item := The_Stack.Top;
> > ...
> 
> That's why I proposed to use package types in my thesis (at
> <http://lglwww.epfl.ch/Team/MW/Ada-Extensions/Ada-Extensions.html>).

I haven't read your thesis, so I can't address any other benefits of this
proposal.  However, you can get the prefix notation in Ada 95, by using a
generic package.

Of course, you don't want to duplicate all the procedures (especially
if using a compiler like GNAT that does macro-expand-type generics!),
so you would write your "package type" as a base package and an
export-view generic package.  This gives you the prefix notation.

It's a bit more work.  If you frequently want this notation, you
should be able to write a preprocessor (I'd use a perl script) to
generate the generic from the base package.

I haven't compiled this example, but it shows the general idea:

    --------------------------------------------------------------
    package Stack_Base is -- typical Ada abstract data type
        type Stack_Type is ...
        function Top (S: Stack_Type) return Item;
        procedure Push (S: Stack_Type; X : in Item);
    end Stack_Base;

    --------------------------------------------------------------
    package body Stack_Base is
    -- left as an exercise for the reader

    --------------------------------------------------------------
    generic
    package Stack is
        function Top return Item;
        Procedure Push (X: in Item);
        pragma Inline (Top, Push);
    end Stack;

    --------------------------------------------------------------
    with Stack_Base; use Stack_Base;
    package body Stack is
        S: Stack_Type;

        function Top return Item is
        begin
            return Top (S);
        end Top;

        procedure Push (X: in Item) is
        begin
            Push (S, X);
        end Push;
    end Stack;

Then you use Stack:

    declare
        package S is new Stack;
        I: Item;
    begin
        S.Push (I);
        if S.Top /= I then ...


> > While we're at it, maybe we can throw in real constructors, too.  We can
> > solve the "problem" with limited types,  by allowing a constructor to be
> > called in the declarative region.  That way limited types wouldn't have to
> > be definate, or go on the heap unnecessarily.
> 
> My proposal for constructors goes like this:
> 
> package type T is
>    procedure new (X : Integer; Y : Float);   -- Note: new is a keyword
>    ...
> end T;
> 
> ...
> 
> Object : T(X => 5, Y => 3.14);

In the Stack example above, we would have in Stack_Base:

    procedure Initialize (S: Stack_Type; X : Integer; Y : Float);

and in the generic spec of Stack:

    generic
       X: Integer;
       Y: Float;
    package Stack is...

and in the body of Stack, in the elaboration section:

    begin
       Initialize (S, X, Y);
    end Stack;

As you say,
> the elaboration of the object declaration creates the object and then
> calls the procedure new with the given parameters.

(replace "object declaration" with "package instantiation.")

> Class types provided this way (any of the above 3 syntax) do not specify
> the mode of the implicit parameter of the class type. This has the nice
> side effect that a function can modify the object on which it is
> applied, which would be very useful in cases like the random number
> generator discussed in another thread.

You can do this by using a procedure in the base package:

    procedure Top (S: in out Stack; I: out Item);

and using that in a function call in the generic:

    function Top return Item is
       I: Item;
    begin
       Top (S, I);
       return I;
    end Top;

Arguments against this approach:
- it's more work
- it doesn't provide other benefits of package types

Arguments in favor of it:
- it provides prefix syntax
- you can do it today in Ada 95

Sam Mize

--
-- Samuel Mize           (817) 619-8622               "Team Ada"
-- Hughes Training Inc.  PO Box 6171 m/s 400, Arlington TX 76005




  parent reply	other threads:[~1997-05-22  0:00 UTC|newest]

Thread overview: 120+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1997-04-23  0:00 Not intended for use in medical, Robert C. Leif, Ph.D.
1997-04-24  0:00 ` J. Florio
1997-04-25  0:00 ` Ken Garlington
1997-04-25  0:00 ` Kevin Cline
1997-04-25  0:00   ` Robert Dewar
1997-04-28  0:00 ` John Apa
1997-04-28  0:00   ` Robert Dewar
1997-04-29  0:00     ` Kaz Kylheku
1997-04-30  0:00       ` John Apa
     [not found]       ` <3367CE1E.5ED1@die_spammer.dasd.honeywell.com>
1997-05-01  0:00         ` Kaz Kylheku
1997-05-03  0:00       ` Simon Wright
1997-05-04  0:00         ` Richard Kenner
1997-05-04  0:00         ` Robert Dewar
1997-05-04  0:00         ` Kaz Kylheku
1997-05-04  0:00           ` Robert Dewar
1997-05-04  0:00             ` Richard Kenner
1997-05-05  0:00             ` Kaz Kylheku
1997-05-06  0:00               ` Kaz Kylheku
1997-05-06  0:00                 ` Robert A Duff
1997-05-07  0:00                   ` Kaz Kylheku
1997-05-08  0:00                     ` Robert A Duff
1997-05-07  0:00                   ` Robert Dewar
1997-05-08  0:00                     ` Robert A Duff
1997-05-09  0:00                       ` Robert I. Eachus
1997-05-11  0:00                         ` Robert Dewar
1997-05-11  0:00                           ` Matthew Heaney
1997-05-12  0:00                             ` Robert Dewar
1997-05-12  0:00                               ` Matthew Heaney
1997-05-13  0:00                                 ` Jon S Anthony
1997-05-13  0:00                                   ` Matthew Heaney
1997-05-14  0:00                                     ` Robert Dewar
1997-05-14  0:00                                     ` Robert Dewar
1997-05-14  0:00                                 ` Robert Dewar
1997-05-15  0:00                                   ` W. Wesley Groleau (Wes)
1997-05-14  0:00                                 ` Robert Dewar
1997-05-14  0:00                                 ` Nick Roberts
1997-05-12  0:00                           ` Robert I. Eachus
1997-05-13  0:00                             ` Robert Dewar
1997-05-14  0:00                               ` Nick Roberts
1997-05-14  0:00                                 ` Robert Dewar
     [not found]                                   ` <01bc6182$30e3a7c0$LocalHost@xhv46.dial.pipex.com>
1997-05-16  0:00                                     ` Robert Dewar
1997-05-16  0:00                                     ` Robert A Duff
1997-05-16  0:00                                       ` Robert I. Eachus
1997-05-18  0:00                                         ` Nick Roberts
1997-05-18  0:00                                           ` Matthew Heaney
1997-05-19  0:00                                             ` Robert I. Eachus
1997-05-19  0:00                                               ` Matthew Heaney
1997-05-20  0:00                                                 ` Nick Roberts
1997-05-20  0:00                                                   ` Matthew Heaney
1997-05-21  0:00                                                     ` Dale Stanbrough
1997-05-19  0:00                                             ` Robert A Duff
1997-05-08  0:00                     ` Kaz Kylheku
1997-05-08  0:00                       ` Robert Dewar
1997-05-09  0:00                         ` Kaz Kylheku
1997-05-12  0:00                       ` W. Wesley Groleau (Wes)
1997-05-12  0:00                         ` John G. Volan
1997-05-14  0:00                       ` Nick Roberts
1997-05-14  0:00                         ` Robert Dewar
1997-05-14  0:00                           ` Jeff Carter
     [not found]                             ` <dewar.863632434@merv>
1997-05-15  0:00                               ` Kaz Kylheku
1997-05-18  0:00                                 ` Robert Dewar
1997-05-15  0:00                         ` Kaz Kylheku
1997-05-08  0:00                     ` John G. Volan
1997-05-10  0:00                       ` Robert Dewar
1997-05-10  0:00                         ` John G. Volan
1997-05-11  0:00                           ` Robert Dewar
1997-05-11  0:00                             ` John G. Volan
1997-05-11  0:00                               ` Robert Dewar
1997-05-11  0:00                             ` John G. Volan
1997-05-11  0:00                               ` Robert A Duff
1997-05-12  0:00                                 ` Robert Dewar
1997-05-12  0:00                                 ` John G. Volan
1997-05-12  0:00                             ` John G. Volan
1997-05-12  0:00                             ` John G. Volan
1997-05-12  0:00                               ` Robert Dewar
1997-05-17  0:00                               ` Robert I. Eachus
     [not found]                                 ` <dewar.863877808@merv>
1997-05-17  0:00                                   ` Robert Dewar
1997-05-17  0:00                                     ` Jon S Anthony
1997-05-21  0:00                                       ` Syntax for tagged record types (was Re: Not intended for use in medical,) Ben Brosgol
1997-05-20  0:00                                         ` Matthew Heaney
1997-05-21  0:00                                           ` Jon S Anthony
1997-05-21  0:00                                             ` Matthew Heaney
1997-05-22  0:00                                               ` Robert I. Eachus
1997-05-25  0:00                                                 ` Matthew Heaney
1997-05-28  0:00                                                   ` Robert I. Eachus
1997-05-23  0:00                                               ` Jon S Anthony
1997-05-23  0:00                                                 ` Matthew Heaney
1997-05-25  0:00                                                   ` Jon S Anthony
1997-05-28  0:00                                                   ` Syntax for tagged record types (was Re David Kristola
1997-05-23  0:00                                                 ` Syntax for tagged record types (was Re: Not intended for use in medical,) Simon Wright
1997-05-21  0:00                                           ` Syntax for tagged record types and class types Mats Weber
1997-05-21  0:00                                             ` Matthew Heaney
1997-05-22  0:00                                               ` Mats Weber
1997-05-27  0:00                                               ` Tucker Taft
1997-05-30  0:00                                                 ` Mats.Weber
1997-05-22  0:00                                             ` Samuel A. Mize [this message]
1997-05-22  0:00                                               ` Samuel A. Mize
1997-05-23  0:00                                               ` Mats Weber
     [not found]                                         ` <mheaney-ya023680002005972314260001@news.ni.net <mheaney-ya023680002105972302430001@news.ni.net>
1997-05-22  0:00                                           ` Syntax for tagged record types (was Re: Not intended for use in medical,) Robert A Duff
1997-05-22  0:00                                             ` John G. Volan
1997-05-23  0:00                                               ` Jon S Anthony
1997-05-23  0:00                                               ` Matthew Heaney
1997-05-23  0:00                                                 ` Jon S Anthony
1997-05-23  0:00                                                   ` Matthew Heaney
1997-05-25  0:00                                                     ` Jon S Anthony
1997-05-25  0:00                                                     ` Robert Dewar
1997-05-27  0:00                                                     ` Ray Blaak
1997-05-23  0:00                                                   ` John G. Volan
1997-05-24  0:00                                                     ` Robert A Duff
1997-05-24  0:00                                                       ` Matthew Heaney
1997-05-28  0:00                                                         ` Robert I. Eachus
1997-05-25  0:00                                                     ` Jon S Anthony
1997-05-12  0:00                         ` Language Design Mistakes (was "not intended...") W. Wesley Groleau (Wes)
1997-05-13  0:00                           ` Robert Dewar
1997-05-13  0:00                             ` Robert A Duff
1997-05-14  0:00                               ` Robert Dewar
1997-05-13  0:00                             ` W. Wesley Groleau (Wes)
1997-04-28  0:00   ` Not intended for use in medical, John M. Mills
1997-04-30  0:00     ` Larry Kilgallen
1997-05-02  0:00       ` Nick Roberts
replies disabled

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