comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <Nick.Roberts@dial.pipex.com>
Subject: Re: How to write TYPECASE in Ada 95?
Date: 1999/02/06
Date: 1999-02-06T00:00:00+00:00	[thread overview]
Message-ID: <79gmmp$eli$4@plug.news.pipex.net> (raw)
In-Reply-To: 1103_918264881@DZOG-CHEN

Given

   type Root_Type is abstract tagged ...;

and

   type Variation_1 is new Root_Type with ...;
   type Variation_2 is new Root_Type with ...;

and you have a particular 'something' you might want to do to an object
which could be either of type Variation_1 or Variation_2, you might find it
clearest to create an abstract procedure

   procedure Something (X: Root_Type; ...) is abstract;

and then override this procedure with

   procedure Something (X: Variation_1; ...) ...

whose body does what is appropriate for objects of type Variation_1, and
then

   procedure Something (X: Variation_2; ...) ...

whose body does what is appropriate for objects of type Variation_2. A call
such as

   Something(A,...);

will then automatically call the correct version of Something, based on the
type of object A. This is called 'dispatching'.

Dispatching generally obviates any need for a TYPECASE construct. However,
it is sometimes the case that all those separate procedures would be
overkill. Given

   type Subvariation_1_1 is new Variation_1 with ...;
   type Subvariation_1_2 is new Variation_1 with ...;

you may have a piece of code which operates on objects of type Variation_1,
but has an extra operation, somewhere in the middle, necessary for objects
of type Subvariation_1_2 (say). In these cases, it is more sensible to use
the 'in' operator to factor out this snippet of code, e.g.

   ...
   if X in Subvariation_1_2'Class then
      ...
   end if;
   ...

You should (almost) always test for membership of a class. E.g. test "X in
Subvariation_1_2'Class" rather than "X in Subvariation_1_2", so that further
descendants of Subvariation_1_2 (if you should add any at a later date) will
still be catered for.

Use the 'in' operator, not 'Tag. The need to use 'Tag is very rare (and
always to do with I/O or interfacing).

-------------------------------------------
Nick Roberts
-------------------------------------------







  reply	other threads:[~1999-02-06  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 [this message]
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         ` robert_dewar
1999-02-19  0:00           ` Tom Moran
1999-02-19  0:00           ` Nick Roberts
1999-02-18  0:00         ` Tom Moran
1999-02-18  0:00         ` Tom Moran
1999-02-18  0:00           ` Matthew Heaney
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             ` (long) programming by extension Samuel Mize
1999-02-25  0:00               ` (shorter and new) " 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