-
-
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
eliminate anytype fields from the language #10705
Comments
There is also the question of how to generate types with |
Ah right, thank you. Amended. |
The |
Maybe the current rendition of Edit: Thinking about it further, is there even a need for being able to retify opaque types with a builtin? The only significant information about an opaque type is its declarations, which can't actually be retified (and afaik isn't planned to be possible?). If you want to generate a unique opaque type with no declarations, you can just write |
If you read this proposal before this comment, read it again because I changed everything (except for the main goal of eliminating anytype fields from the language). |
no, there is not.
|
Would that also be equivalent to |
|
Using .{ .name = "a", .type = ?u32, .default_value = null },
.{ .name = "b", .type = ?u32, .default_value = &@as(?u32, null) }, |
Getting the default value needs an const default_value = @ptrCast(*const field.type, @alignCast(@alignOf(field.type), field.default_value.?)).*; Could probably use a helper method for doing this :) |
Motivation
Simplify the language. Most people probably aren't even aware that this is possible:
Similarly, the simplified language will be simpler to implement compilers for. Such as, you know, the one that is the main focus of the 0.10.0 milestone.
Proposed changes
Mainly, make it a compile error to use
anytype
as a field name. However this poses some problems to solve, such as how to deal with@typeInfo
for various things. For example, the sentinel field of pointers:https://github.com/ziglang/zig/blob/0.9.0/lib/std/builtin.zig#L239-L252
Likewise the
sentinel
field of arrays, thedefault_value
field of structs, and thefunction
field of async frames. But that's it. I can't find any other dependencies on this feature in the entire standard library.In order to address this problem, this proposal suggests to use compile-time type erasure by using
?*const anyopaque
instead ofanytype
. This is something that the language is already required to support, so we are exploiting it here rather than introducing yet another way of doing the same thing viaanytype
struct fields.Example of printing the default value for the first field of a struct:
Example of constructing an array type:
Related Issues:
anytype
a full (comptime
-only) type #9107The text was updated successfully, but these errors were encountered: