From: Matthew Heaney <matthew_heaney@acm.org>
Subject: Re: How to write TYPECASE in Ada 95?
Date: 1999/02/18
Date: 1999-02-18T00:00:00+00:00 [thread overview]
Message-ID: <m3hfsk79le.fsf@mheaney.ni.net> (raw)
In-Reply-To: 36cb2712.1390453@news.pacbell.net
tmoran@bix.com (Tom Moran) writes:
> Dispatching is certainly better than a case statement - if you use it.
>
> Consider an existing package of the form:
>
> package Grocer is
> type Fruit is abstract tagged ...
> type Apple is new Fruit ...
> ...
> type Watermelon is new Fruit ...
> ...
> function Best(Budget : in Money) return Fruit'class;
> end Grocer;
>
> And now you are writing a new package which, among other things, does
>
> with Grocer;
> use Grocer;
> package Meal is
> procedure Serve(Dish : in Apple);
> ...
> procedure Serve(Dish : in Watermelon);
> end Meal;
>
> Suppose a user wants, not unreasonably, to write
> ...
> Meal.Serve(Dish => Grocer.Best(Budget=>1.00));
>
> Unless you can go back and modify Grocer, it appears you must include
> in Meal
> procedure Serve_Fruit(Dish : in Fruit'class);
> with a body like
> procedure Serve_Fruit(Dish : in Fruit'class) is
> begin
> if Dish in Apple'class then Serve(Apple(Dish));
> ...
> elsif Dish in Watermelon'class then Serve(Watermelon(Dish));
> else raise Heck;
> end if;
> end Serve_Fruit;
>
> Is there a better way?
It seems like the real problem in your example is that Fruit doesn't
include any primitive operations for Serve'ing.
If you want to extend a type with arbitrary behavior, you could take a
look at using the Visitor pattern. Ada implementations of this pattern
are available at the ACM patterns archive.
<http://www.acm.org/archives/patterns.html>
next prev parent reply other threads:[~1999-02-18 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 [this message]
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? Matthew Heaney
1999-02-06 0:00 ` 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.
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox