From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: * X-Spam-Status: No, score=1.2 required=5.0 tests=BAYES_00,FROM_WORDY, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,47def5aa7b3182bd X-Google-Attributes: gid103376,public From: "Nick Roberts" Subject: Re: How to write TYPECASE in Ada 95? Date: 1999/02/06 Message-ID: <79gmmp$eli$4@plug.news.pipex.net>#1/1 X-Deja-AN: 441273288 References: <79fct8$9k3$1@murdoch.acc.Virginia.EDU> <1103_918264881@DZOG-CHEN> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Organization: UUNET WorldCom server (post doesn't reflect views of UUNET WorldCom) Newsgroups: comp.lang.ada Date: 1999-02-06T00:00:00+00:00 List-Id: 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 -------------------------------------------