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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,f34a46f284c01816 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!news.zanker.org!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!newsfeed1.ip.tiscali.net!tiscali!transit1.news.tiscali.nl!dreader2.news.tiscali.nl!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Base'Class is instance of Der1? References: <1105185677.254129.171840@c13g2000cwb.googlegroups.com> From: Ludovic Brenta Date: Sat, 08 Jan 2005 13:51:00 +0100 Message-ID: <87llb4krt7.fsf@insalien.org> User-Agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:ckbgScNnrY34jLnZeWB2fgT1fFk= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Tiscali bv NNTP-Posting-Date: 08 Jan 2005 13:51:00 CET NNTP-Posting-Host: 83.134.239.204 X-Trace: 1105188660 dreader2.news.tiscali.nl 44081 83.134.239.204:38612 X-Complaints-To: abuse@tiscali.nl Xref: g2news1.google.com comp.lang.ada:7565 Date: 2005-01-08T13:51:00+01:00 List-Id: "R" writes: > Hello. > > I've got function which has a parameter of type Base'Class. > > well I have 3 derived tagged records and depending of what exactly is > the parameter I have to cast it to a proper function. > > How can I figure out whether or not given object is der1 or der2 or > der3 type? > > thanks in advance > > best regards > R This is called "dispatching", and Ada 95 supports it directly in the language. There is no need for you to explicitly check the actual type: package P is type Base is tagged abstract null record; procedure Proc (B : in Base) is abstract; type Der1 is new Base with null record; procedure Proc (D : in Der1); type Der2 is new Base with null record; procedure Proc (D : in Der2); type Der3 is new Base with null record; procedure Proc (D : in Der3); end P; with P; procedure Dispatch (B : in P.Base'Class) is begin P.Proc (B); -- dispatches to the appropriate Proc end Dispatch; To make this work, you must make sure that each instance of Proc is a "primitive operation" of the appropriate type. A "primitive operation" is: - declared in the same package as the type - has at least one parameter or return value of the type Primitive operations are defined in detail in ARM 3.2.3. And you have to make sure that the point where you call P.Proc is a "dispatching call". For the call to be dispatching, the actual parameter must be of a class-wide type, here Base'Class. The precise rules for dispatching calls are in ARM 3.9.2. Now, if you really insist on checking the actual type yourself: with P; procedure Dispatch (B : in P.Base'Class) is begin if B in P.Der1'Class then -- dispatch elsif B in P.Der2'Class then -- dispatch ... end Dispatch; This is much more error-prone and less maintainable than language-assisted dispatching. I do not recommend this way. -- Ludovic Brenta.