TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Tagged unions, or ‘variants’, in C

30 pointsby Proceduralalmost 8 years ago

3 comments

klodolphalmost 8 years ago
No, no, no! This is terrible!<p>variant_cast is just garbage. It relies on struct layout, and if anything has wider alignment than ptrdiff_t, it&#x27;s broken. For example, a double in 32-bit PowerPC has 64-bit alignment but ptrdiff_t is 32 bits, so the pointer is wrong. Maybe x64 you get away with it for most types (not all!), but can you raise your right hand and solemnly swear that you&#x27;ll never ever want to port to a non-x64 system?<p>Additionally, all the explicit casting means that this has basically no type safety at all. I can a.tag = tag(SomeThree) even though SomeThree is not a member of a&#x27;s type, and then I will be able to successfully let b = as(&amp;a, SomeThree) and boom! I&#x27;ve just scribbled over memory without so much as a peep from the compiler. What is the of having tagged unions (a TYPE SAFETY FEATURE) if your tagged union implementation is LESS type safe than untagged unions?<p>This last point is actually a bit subtle… accessing the wrong member of a union is probably not what you want to do most of the time, but the language in the C standard is quite clear (see DR #257, language was clarified in C99 but present in earlier versions) and accessing the wrong union member just gives you whatever you stored in a union reinterpreted as whatever you read out of it. But this tagged union implementation lets you access a union member which doesn&#x27;t even exist.<p>Let&#x27;s not try to implement new language features on top of C with macros. It never ends well. Either figure out a way to live with a little boilerplate or use a different language, those are the only sane options.
评论 #14635850 未加载
评论 #14636158 未加载
评论 #14635677 未加载
评论 #14635866 未加载
评论 #14636446 未加载
k__almost 8 years ago
Is there a reason, other than jargon, why this concept is called different names in different languages?<p>Do variants work different than tagged unions or type classes?<p>Some people told me that Haskell is superior in FP because of its type classes. Later I read TypeScript has tagged unions, which looked like type classes to me.<p>Then I looked into Reason and they wrote about variants and I also was reminded of type classes.
评论 #14635655 未加载
评论 #14635730 未加载
评论 #14635592 未加载
评论 #14635602 未加载
valbacaalmost 8 years ago
Any drawbacks of using these #defines? It looks awesome<p><pre><code> #define var __auto_type #define let __auto_type const</code></pre>