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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,63fa88e2f1a3ebea X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!newsread.com!news-xfer.newsread.com!nntp.abs.net!news-FFM2.ecrc.net!noris.net!newsfeed.arcor.de!news.arcor.de!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: Universal type in Ada Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.14.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: Date: Wed, 15 Jun 2005 15:55:15 +0200 Message-ID: <1a7ughq9xrk30.1q7sknowaq5or.dlg@40tude.net> NNTP-Posting-Date: 15 Jun 2005 15:55:15 MEST NNTP-Posting-Host: e53e289c.newsread2.arcor-online.net X-Trace: DXC=kJEn4_8da:iCUhGd:LVK2eQ5U85hF6f;djW\KbG]kaMhFYk:AnJB[Cm_KmVObShjZb[6LHn;2LCVn7enW;^6ZC`d<=9bOTW=MNn X-Complaints-To: abuse@arcor.de Xref: g2news1.google.com comp.lang.ada:11373 Date: 2005-06-15T15:55:15+02:00 List-Id: On 15 Jun 2005 06:07:12 -0700, zw wrote: > Hi, I am trying to create a Universal type in Ada such as "Object" in > Java so that I could define a function that returns a value in this > Universal type, then I could lower-cast value in this type to its > specific type such as String, Float or any user-defined types. Because > in Java if a method returns an Object, it could be casted to any other > types that are subtypes of Object. Is there a Universal type in Ada? Yes. What you describe is a contravariant result. In Ada it is achieved by returning a class-wide result. A function of this sort is also called "abstract factory". > Could anyone tell me how to create such types in Ada, please? type Object is abstract tagged null record; function Factory (...) return Object'Class; type My_Special_Object is new Object with ...; declare X : My_Special_Object; begin ... X := My_Special_Object Factory (...)); Here the result of Factory is explicitly converted to My_Special_Object. If that is impossible (because the result is not a My_Special_Object) Constraint_Error exception is propagated at run-time. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de