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: Alex // nytpu Newsgroups: comp.lang.ada Subject: Re: Best use cases for OOP (resent quoted sorry) Date: Thu, 28 Aug 2025 14:23:58 -0600 Organization: A noiseless patient Spider Message-ID: <108qdsu$1j3g1$1@dont-email.me> References: <108pppp$1dmp6$1@dont-email.me> <108pq1l$1dp6u$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 28 Aug 2025 20:23:58 +0000 (UTC) Injection-Info: dont-email.me; posting-host="e35781679b937d1430900e367383f858"; logging-data="1674753"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/p8zTUz8IqP7rf2WMxrlRA" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:jDuF0sMnvrvshiCpg/NMJNnb0Q8= In-Reply-To: <108pq1l$1dp6u$1@dont-email.me> Content-Language: en-US, en-US-large Xref: feeder.eternal-september.org comp.lang.ada:66979 List-Id: On 8/28/25 8:45 AM, Kevin Chadwick wrote: > <...> > > I have struggled to find compelling reasons to use tagged types considering > they affect the size of records and potentially elaboration issues that > cannot exist without tagged types. > > I know some use tagged types just for dot notation but that doesn't really > move the needle in my mind. I would certainly appreciate it if people could > share their favourite use cases for tagged types though. Things that are > perhaps a pain without them or were with Ada 83. In general, things like variant records are sorta "perpendicular" in use to OOP/tagged types. Tagged types make it easy to add a new class while not having to modify most of the routines you can use on the class, but it's tedious to add new routines; while variant records make it easy to add new routines to use on the type but it's tedious to add a new record variant. Linking to a random section of a random book but this section is a great explanation: In Ada in particular, my main preferred use-case is defining interfaces so my routines can take a `My_Interface'Class` type and then anything needing to use them can just implement the interface in whatever record the want to use my code with. This seems to be one of the primary things the Ada standard library uses it for too, e.g. streams, storage pools, and controlled types. Note that I'm saying "interface" as a concept distinct from Ada's interfaces, for instance the standard library makes controlled types and storage pools be full tagged types you subclass (for various complex reasons) but conceptually they're still just empty types defining a common interface for the routines those APIs need. Although admittedly it is extremely rare I use (non-derived) tagged types at all though, and when I do it is almost exclusively to get dot notation lol (and tagged types were supposed to no longer be needed for dot calls in Ada 2022 but it was delayed until Ada 202y). I do use types derived from the standard library types a lot though; namely controlled types for bindings and for custom stream types. I wouldn't really worry about the "overhead" of tagged types not significant on any modern system and no more than a language like C++ (let alone high-level dynamic languages) add to literally every data structure with no option to omit the metadata like Ada has. The only real issue is that IIRC tagged types are always at least twenty bytes on GNAT (on ARMv4T/ARMv5T at least, since developing for them is the only time I've gotten warnings about this limitation) which precludes things like having a record be small enough to be passed in a register instead of the stack, but not much of an issue on modern systems. It'd also preclude things like a record containing a single field being optimized to acting like a variable directly holding that field type since there's the tag overhead. Very premature micro-optimization-y concern still though IMO, just use tagged types when it makes sense. ~nytpu -- Alex // nytpu https://nytpu.com/ - gemini://nytpu.com/ - gopher://nytpu.com/