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.3 required=5.0 tests=BAYES_00,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: Matthew Heaney Subject: Re: How to write TYPECASE in Ada 95? Date: 1999/02/06 Message-ID: #1/1 X-Deja-AN: 441253879 Sender: matt@mheaney.ni.net References: <79fct8$9k3$1@murdoch.acc.Virginia.EDU> NNTP-Posting-Date: Fri, 05 Feb 1999 20:53:28 PDT Newsgroups: comp.lang.ada Date: 1999-02-06T00:00:00+00:00 List-Id: nr@labrador.cs.virginia.edu (Norman Ramsey) writes: > I'm trying to figure out from the reference manual how to identify > which type extension of a type I have. For example, if I have > > type Point is tagged > record > X, Y : Real := 0.0; > end record; > > type Painted_Point is new Point with > record > Paint : Color := White; > end record; > > > type Tall_Point is new Point with > record > Height : Real := 0.0; > end record; > > I'd like to be able to discriminate at run time: > > p : Point; > > ... > > if p in Painted_Point then > ... do something with Painted_Point'(p) > else if p in Tall_Point then > ... do something with Tall_Point'(p) You've got it right: use a membership test. The only problem here is that you should have said "elsif" instead of "else if". > Even better would be something like Modula-3 TYPECASE: > > (* modula-3 *) > TYPECASE p OF > Painted_Point (painted) => ... do something with painted > Tall_Point (tail) => ... do something with tall > ... > END No, you can't use case statement for this sort of thing. > What's the idiomatic way to express this in Ada? The idiomatic way in Ada --and in any object-oriented language-- is to not do this at all. You should be call a primitive operation of the type that automatically dispatches according to the (type) tag of the object. If you insist on doing an explicit test, then use a membership test.