comp.lang.ada
 help / color / mirror / Atom feed
From: Samuel Mize <smize@imagin.net>
Subject: Re: (long) programming by extension
Date: 1999/02/24
Date: 1999-02-24T00:00:00+00:00	[thread overview]
Message-ID: <7b22f5$1mrg@news1.newsguy.com> (raw)
In-Reply-To: 7b1kdb$13k6@news3.newsguy.com

The example I used to describe programming by extension is also used
to describe object-oriented programming.  So it was a poor choice to
show the difference.  However, there IS a difference.

I'll try again.

Suppose you are coding a message handler which will get messages in
one format from many interfaces, some of which are not yet known, and
perform some complex computation.  You can define each interface to
have a "get" procedure, and use a tagged type to pick the right "get."

    package No_Interface is
        type Use_None is tagged null record;
        type Interface_Selector is access all Use_Nothing'Class;

        -- this will raise an exception if called,
        -- or maybe return a default value

        procedure Get (Interface: Use_Nothing;
                       Message: Data_Format -- defined elsewhere
                       );

    end No_Interface;
    - - - - - - - - - - - - - - - - - - - - -
    with No_Interface;
    package Disk_Interface is
        type Use_Disk is new No_Interface.Use_None with null record;

        procedure Get (Interface: Use_Disk;
                       Message: Data_Format);
    end package;
    - - - - - - - - - - - - - - - - - - - - -
    with No_Interface;
    package Serial_Interface is
        type Use_Serial is new No_Interface.Use_None with null record;

        procedure Get (Interface: Use_Serial;
                       Message: Data_Format);
    end package;
    - - - - - - - - - - - - - - - - - - - - -
    with No_Interface; use No_Interface;
    procedure Message_Handler (Current: Interface_Selector) is
       D: Data_Format;
    begin
       -- some complex computation involving several records
       for I in whatever_range loop
          Get (Current.all, D);
            ...
            if D.Continued then
                Get (Current.all, D);
                ...
       end loop;
    end Message_Handler;
          
In no case will the type Use_[whatever] contain any data.

You can add interfaces as you need to, and never have to recode -- or
even re-compile -- Message_Handler.

I would certainly NOT call this object-oriented programming.  It's sort
of an "infinite case statement."  But it does take advantage of the
Ada 95 facilities for programming by extension.

In a real-world application, something as significant as a major device
interface probably WOULD be coded as an "object," if you're using
object-oriented technology.  This is a contrived example.

I HAVE used tagged types to generate such an "infinite case statement,"
but it was buried in an obscure and proprietary system, so it would be
neither clear nor legal for me to reproduce the code.

Best,
Sam Mize

-- 
Samuel Mize -- smize@imagin.net (home email) -- Team Ada
Fight Spam: see http://www.cauce.org/ \\\ Smert Spamonam




  reply	other threads:[~1999-02-24  0:00 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-02-05  0:00 How to write TYPECASE in Ada 95? Norman Ramsey
1999-02-05  0:00 ` Brian Rogoff
1999-02-05  0:00   ` David C. Hoos, Sr.
1999-02-05  0:00     ` Brian Rogoff
1999-02-06  0:00   ` Ed Falis
1999-02-06  0:00     ` Nick Roberts
1999-02-06  0:00       ` Nick Roberts
1999-02-17  0:00     ` Tom Moran
1999-02-18  0:00       ` Matthew Heaney
1999-02-18  0:00         ` Tom Moran
1999-02-18  0:00           ` Matthew Heaney
1999-02-18  0:00         ` Tom Moran
1999-02-18  0:00         ` robert_dewar
1999-02-19  0:00           ` Nick Roberts
1999-02-19  0:00           ` Tom Moran
1999-02-19  0:00     ` Tom Moran
1999-02-19  0:00       ` Tom Moran
1999-02-23  0:00       ` Samuel Mize
1999-02-23  0:00         ` Question (was Re: How to write TYPECASE in Ada 95?) Mike Silva
1999-02-24  0:00           ` Nick Roberts
1999-02-24  0:00           ` (long) programming by extension (was: " Samuel Mize
1999-02-24  0:00             ` Samuel Mize [this message]
1999-02-25  0:00               ` (shorter and new) programming by extension Samuel Mize
1999-02-25  0:00                 ` Mike Silva
1999-02-26  0:00                   ` Samuel Mize
1999-02-24  0:00           ` Question (was Re: How to write TYPECASE in Ada 95?) Samuel T. Harris
1999-02-24  0:00             ` Matthew Heaney
1999-02-24  0:00               ` Tucker Taft
1999-02-06  0:00 ` How to write TYPECASE in Ada 95? David C. Hoos, Sr.
1999-02-06  0:00   ` Matthew Heaney
1999-02-06  0:00     ` Matthew Heaney
1999-02-06  0:00     ` Matthew Heaney
1999-02-09  0:00     ` David C. Hoos, Sr.
1999-02-06  0:00 ` Matthew Heaney
replies disabled

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