site banner

Friday Fun Thread for January 31, 2025

Be advised: this thread is not for serious in-depth discussion of weighty topics (we have a link for that), this thread is not for anything Culture War related. This thread is for Fun. You got jokes? Share 'em. You got silly questions? Ask 'em.

2
Jump in the discussion.

No email address required.

Rust does have normal C++-style unions, though they're a late and fairly controversial addition for the reasons you mention. I'll admit that I've used them occasionally in internal code (especially networking or protocol development, where hardware developers love throwing in 'this next four bytes could be an int or a float' in rev 1.0.1a after you've built your entire reader around structs), but I'd probably ask anyone who used them as an input in an API what they were smoking.

In higher-abstraction languages than C++, that sorta behavior either isn't available and/or forgo the performance and memory-specific benefits for dumb-programmer-safety. TypeScript unions or Java sealed interfaces are doing the same thing at the level of a definition -- it's a field you can put any of a limited number of options in! -- and you'd absolutely never use them for overlapping purposes. On the other hand, C#'s even more limited than Java on that use case, and I come across places it'd make sense to use pretty regularly, so maybe I'm just bitching.

That may be why a lot of their more type-theory focused stuff fell under a different name than the TypeScript-style union types.

I think the Rust enum overload is downstream of a lot of the behaviors you'll see in Java or Kotlin; I've encouraged FIRST students to use similar designs to hold different configuration values for easy toggling of modes or states. Not sure who first made enums that broke from the C(++) limit of one-value-per, but given the amount of C++ code I've seen where enums were used to map various flags intended for bitwise addition, probably a pretty early one.

(especially networking or protocol development, where hardware developers love throwing in 'this next four bytes could be an int or a float' in rev 1.0.1a after you've built your entire reader around structs)

On behalf of hardware developers everywhere, I apologize. We didn't want to do that either, but when the potential new customer opines "what a nice piece of hardware you've got, if only it could take a float" and glances meaningfully at their suitcase full of cash... well, we shake our heads and roll up our sleeves.