From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on ip-172-31-91-241.ec2.internal X-Spam-Level: X-Spam-Status: No, score=0.0 required=3.0 tests=none autolearn=ham autolearn_force=no version=4.0.1 Path: nntp.eternal-september.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: In precision typing we trust Date: Thu, 28 Aug 2025 10:24:54 +0200 Organization: A noiseless patient Spider Message-ID: <108p3om$16qn1$3@dont-email.me> References: <107uv9g$3019a$1@dont-email.me> <107v1ji$303of$1@dont-email.me> <336fbb5f-a279-ea8e-67fd-f62bb00d6a89@irrt.De> <107vfb9$34cpj$1@dont-email.me> <10855lq$gj8l$1@dont-email.me> <1088h1a$19635$1@dont-email.me> <1089p1i$1ig1d$1@dont-email.me> <108aq2p$1qo9o$1@dont-email.me> <108b1r3$1sj3c$1@dont-email.me> <108dh4t$2f5h3$2@dont-email.me> <108dkik$2g20p$1@dont-email.me> <108g1cg$32gqg$2@dont-email.me> <108h6b0$3a75k$2@dont-email.me> <108iiq5$3lihe$3@dont-email.me> <108mhhk$j2jt$1@dont-email.me> <108mis1$j4cj$1@dont-email.me> <108o33p$vok4$5@dont-email.me> <108o6rp$10njb$1@dont-email.me> <108o7cm$10qct$1@dont-email.me> <108o845$10pj9$3@dont-email.me> <108oann$115to$1@dont-email.me> <108occm$11h9j$2@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 28 Aug 2025 08:24:54 +0000 (UTC) Injection-Info: dont-email.me; posting-host="6f970729dc22bef6d204ec8c3c6ab955"; logging-data="1272545"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19xmn9wOtI+FdQbXOQVTftn1zt2N6KZe3A=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:7jkFe6Zb1Zj/PBOXNX9gx7C3dPk= In-Reply-To: <108occm$11h9j$2@dont-email.me> Content-Language: en-US Xref: feeder.eternal-september.org comp.lang.ada:66962 List-Id: On 2025-08-28 03:45, Lawrence D’Oliveiro wrote: > But the function has to return the base class type, it cannot have > different return types dynamically. Of course you can. That is the meaning of the term "class" and "class type" as a type which values are the values of the class members. In Ada class type is denoted as T'Class. T'Class values are values of T or any type derived from T. The operations declared on T'Class are called class-wide, so function F (...) return T'Class; > Note how you have a switch-expression that uses the value of the > event-type field to return the corresponding type object, and I cast > the info pointer to point to that type. And then the “additional” > variable-length array can contain pointers to additional data, > depending on the event type. That is not much OO as you have all alternatives static. An Ada programmer might tend to use a variant record and enumeration for the case. But for the OO approach: function F return Abstract_Packet'Class is begin case Kind_Of is when Foo => return Foo_Packet; -- Derived from Abstract_Packet when Bar => return Bar_Packet; -- Derived from Abstract_Packet ... Note also that one can return instances not knowing the specific type at all. E.g. you can dispatch on the bare tag or an argument. In the latter case the derived type registers itself in a mapping value -> constructing function. The client simply calls the function from the mapping and gets a class-wide object. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de