comp.lang.ada
 help / color / mirror / Atom feed
From: cis.ohio-state.edu!pacific.mps.ohio-state.edu!math.ohio-state.edu!howland
Subject: Re: Membership tests for tagged types
Date: 28 Jun 93 08:47:00 GMT	[thread overview]
Message-ID: <1993Jun28.091935@lglsun.epfl.ch> (raw)

In article <mcook.741125871@fendahl.dev.cdx.mot.com>, mcook@fendahl.dev.cdx.mot
.com (Michael Cook) writes:
: stt@spock.camb.inmet.com (Tucker Taft) writes:
: 
: [...]
: >the general feeling was that a general OO principle
: >is that "case statements are harmful."
: 
: Hmm.  What's wrong with case statements?

There is nothing wrong with case statements from the OO point of view.
Where OO purists have a problem is when you use a discriminant record
and then implement the operations with case statements on the
discriminant.  This is more cleanly implemented through inheritance and
dispatching which makes the code clearer and more maintainable.
However, in Ada 83 we don't have full support for inheritance and no
support for dispatching so we are stuck with case statements and
discriminant records for the time being.

: 
: [...]
: >If I understand your example, it would be written as follows:
: 
: >   if X in A'Class then ...
: >   elsif X in B'Class then ...
: >   else ...
: >   end if;
: 
: >Admittedly not as readable as a "case" statement, but
: >it gets the job done and it keeps the language itself simpler.

I don't think there is really enough information in this example to be
really meaningful. It could be a case where one shouldn't even be using
the class attribute and operator overloading would handle everything
just fine. Or it could be better solved with inheritance and
dispatching.

Here is a case that might be interesting.

Given the type hierarchy

             Dog
              |
     ____________________
     |        |          |
                 
Big_Dog  Medium_Dog Little_Dog

and a stack of Dog'Class objects (a heterogenous stack of Big, Medium and
Little Dogs)

Suppose we want to sort the stack into three homogeneous stacks.

could we just write three insert procedures

   procedure Insert( The_Dog : Big_Dog);
   procedure Insert( The_Dog : Medium_Dog);
   procedure Insert( The_Dog : Little_Dog);
 
one for each stack and then write some sort of loop around
 
   Insert( The_Dog => Pop( The_Stack => Mixed_Dog_List ));
   
where Pop returns the Dog'Class object off the top of the stack?  Here
I would be hoping that the compiler could determine which insert to
call at runtime based on the type of the Dog'Class object.  However
Insert is not a primitive operation of the type Dog so I'm not at all
sure if this is possible.  The alternative would be either a case or
sequence of if statements along with membership tests or restructuring
the code to make Insert a primitive of Dog.

Robb

             reply	other threads:[~1993-06-28  8:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1993-06-28  8:47 cis.ohio-state.edu!pacific.mps.ohio-state.edu!math.ohio-state.edu!howland [this message]
  -- strict thread matches above, loose matches on Subject: below --
1993-06-26 14:50 Membership tests for tagged types howland.reston.ans.net!noc.near.net!inmet!spock!stt
replies disabled

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