-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A type-safe representation of tagged unions #1
Comments
Hello, thank you for your interest. Your C preprocessor metalanguage project is very impressive! I tried to do something along those lines in GNU Make once and it quickly became unmanageable.
Compilers provide warnings in this case. When For example,
This is definitely a huge benefit, certainly something that I want. However I'm not sure it's enough to justify importing an external dependency into lone. I'll have to try it out to fully evaluate it but from a cursory exploration of your projects I think it would make Your point did not fall on deaf ears though. I think compilers should be able to provide warnings for this. It would be neat if we could apply some kind of annotation to each struct lone_value {
/* ... */
enum lone_type type;
union {
struct lone_module module __attribute__((tag, type, LONE_MODULE));
struct lone_function function __attribute__((tag, type, LONE_FUNCTION));
struct lone_primitive primitive __attribute__((tag, type, LONE_PRIMITIVE));
/* ... */
};
}; Then the compiler would be able to warn if the I don't know how to add this feature to the C compilers but I'm going to look for the proper upstream channels to suggest/request it. Such a feature would improve safety for all C projects. |
I was a bit unclear in my initial statement. The explicit tagged union style encourages us to deal with tagged unions explicitly; as a result, it can be tempting to just write The check you've mentioned is called exhaustiveness analysis. It is done by modern compilers both for explicit tagged unions and for
The only downside I see is a slight complication of the build process. Also, there are scripts that turn header-only libraries into a single header (SQLite uses it IIRC) for easier inclusion.
I agree, it can be a nice feature. Let me know how it goes with requesting it. Anyhow, feel free to comment here and ask any further questions. |
Alright, I opened issues on both GCC 112840 - feature request: warn on incorrect tagged union value access |
Hi there! I came from your Reddit post and found this project fascinating.
While reading the sources, I've found the header
include/lone/types.h
, which uses a common technique of tagged unions. Most notably,struct lone_value
:The downside of this approach is that it's possible to mess up during case analysis by 1) not checking a tag, or by 2) checking a tag
A
and then usingB
. The compiler cannot check this automatically, causing the bug to silently creep into the final executable.Instead of managing tagged unions manually, I would suggest using Datatype99, which is a header-only library designed specifically to deal with the problem of tagged unions.
struct lone_value
would look as follows 1:And case-analyzed as follows:
Since
of
explicitly provides a variable binding, such asmodule
orfunction
, it's now much harder to make a mistake. Thedatatype
encoding is also more concise than the corresponding tagged union representation, since the former defines bothenum lone_type
andstruct lone_value
2.Since Datatype99 has no run-time dependencies (not even the C standard library), and has a transparent and formally specified semantics, I think it might be a great fit for Lone 3.
Let me know if you have any thoughts or questions, which I should be able to answer.
Footnotes
Auxiliary data can be added as a separate structure. ↩
Although both types can be manipulated separately under the names
LoneValueTag
andLoneValue
. ↩Other tagged unions in the project can be rewritten in the same way, if there are any. ↩
The text was updated successfully, but these errors were encountered: