-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
raw unions #144
Comments
If the purpose is for C interop, then there are other factors. Take the following example:
If you are on a 64-bit system with 32-bit ints and 64-bit pointers, then the value you get for 'a' will depend on the alignment requirements and byte order of the CPU and compiler. To make such a union interoperable between Zig and C, you would need to have Zig match the byte ordering and alignment used by the C compiler. In practice the byte order is probably not as big of a problem, but the alignment might be different. |
It's an open question what a non-extern union will look like. |
Are there many use cases for a non-extern union that is not enum? |
Here's the current plan for unions:
Unions do not side-step Type Based Alias Analysis |
I ran into an LLVM bug with debug info in 5.0.0 when implementing this.
I'm looking for a workaround. |
Non-extern unions will have the following safety feature:
|
if you do this: const Foo = union {
name1: u32,
name2: f64,
};
test "aoeu" {
var foo = Foo { .name2 = 12.34 };
bar(&foo.name1);
}
fn bar(x: &u32) {
*x = 1234;
} How is zig supposed to know how to write the secret field indicating I'll tell you how. Accessing union fields by name will only give a const pointer to If you want a mutable pointer to a union field, you do this: test "aoeu" {
var foo = Foo { .name2 = 12.34 };
bar(@mutableUnionPtr(foo, "name1"));
}
|
More adjustments: Nevermind about
|
Zig already supports tagged unions (#5) but for better C compatibility Zig needs to support C-style unions as well. Also, tagged unions have the tag field, and if programmers don't want that field, they're out of luck. Supporting raw unions is additional power that is needed to compete with C.
However, we want to encourage programmers to try to use tagged unions (enum) instead. It's not really possible for debug instrumentation to protect against the dangers of unions.
The text was updated successfully, but these errors were encountered: