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: Lawrence =?iso-8859-13?q?D=FFOliveiro?= Newsgroups: comp.lang.ada Subject: Re: In precision typing we trust Date: Thu, 28 Aug 2025 01:45:58 -0000 (UTC) Organization: A noiseless patient Spider Message-ID: <108occm$11h9j$2@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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Thu, 28 Aug 2025 01:45:59 +0000 (UTC) Injection-Info: dont-email.me; posting-host="89032bd5e4965475471761f4f9f63eba"; logging-data="1099059"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19AGBLCN2urc0nsm+YfH5h1" User-Agent: Pan/0.163 (Kryvyi Rih) Cancel-Lock: sha1:6iOn4VYo8pClkRD4jFtCZzrP/eE= Xref: feeder.eternal-september.org comp.lang.ada:66959 List-Id: On Wed, 27 Aug 2025 19:17:43 -0600, Alex // nytpu wrote: > On 8/27/25 6:33 PM, Lawrence D’Oliveiro wrote: >> >> But can you do type checking dynamically, at run-time? >> >> For example, in one project (a Python wrapper for receiving >> fanotify events from the Linux kernel), I had to deal with a >> sequence of packets of variable length and type. I could dispatch >> on the packet-type field and return correspondingly typesafe >> results in my wrapper. > > Yes, that's sorta the entire premise of Ada's OOP lol. Use the > `'Class` type to give you a root class and then you can dispatch > with a method call or you sorta can match directly (nowhere even > near the convenience of more modern functional-style matching > admittedly though). Or just have a variant record that you can match > on with a `case` if one prefers---I usually do this unless I need > dynamic dispatch or inheritance. But the function has to return the base class type, it cannot have different return types dynamically. E.g. in the above case, I have an expression like info_type = infoptr.contents.info_type infoptr = ct.cast \ ( infoptr, ct.POINTER ( { FANOTIFY.EVENT_INFO_TYPE_FID : FANOTIFY.event_info_fid, FANOTIFY.EVENT_INFO_TYPE_DFID_NAME : FANOTIFY.event_info_fid, FANOTIFY.EVENT_INFO_TYPE_DFID : FANOTIFY.event_info_fid, FANOTIFY.EVENT_INFO_TYPE_PIDFD : FANOTIFY.event_info_pidfd, FANOTIFY.EVENT_INFO_TYPE_ERROR : FANOTIFY.event_info_error, }[info_type] ) ) additional.append(infoptr) if info_type == FANOTIFY.EVENT_INFO_TYPE_DFID_NAME : nameptr = ct.cast \ ( ct.cast(infoptr, ct.c_void_p).value + ct.sizeof(FANOTIFY.event_info_fid) + infoptr.contents.handle.handle_bytes, ct.c_char_p ) additional.append(nameptr.value) #end if 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. > But in the specific case of interacting directly with hardware[1], > Ada is far and away the best language in existence for it. Look at MicroPython, though. It makes it so easy to write just a few lines of code to twiddle some bits or something. Ada is for the big stuff, not the small stuff.