From: Alex // nytpu <nytpu@example.invalid>
Subject: Re: Best use cases for OOP (resent quoted sorry)
Date: Thu, 28 Aug 2025 14:23:58 -0600 [thread overview]
Message-ID: <108qdsu$1j3g1$1@dont-email.me> (raw)
In-Reply-To: <108pq1l$1dp6u$1@dont-email.me>
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:
<https://craftinginterpreters.com/representing-code.html#the-expression-problem>
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/
next prev parent reply other threads:[~2025-08-28 20:23 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-28 14:40 Best use cases for OOP Kevin Chadwick
2025-08-28 14:45 ` Best use cases for OOP (resent quoted sorry) Kevin Chadwick
2025-08-28 15:12 ` Dmitry A. Kazakov
2025-08-28 18:28 ` Niklas Holsti
2025-08-28 20:23 ` Alex // nytpu [this message]
2025-08-29 8:28 ` Dmitry A. Kazakov
2025-08-29 9:14 ` Kevin Chadwick
2025-08-29 10:37 ` Dmitry A. Kazakov
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox