-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
comptime struct fields #3677
Comments
See #208 (comment) for @Hejsil's alternate proposal for how to solve this problem. |
This is a good read: https://guide.elm-lang.org/appendix/types_as_sets.html When you view types as "sets of values", it's true that any type that only has 1 possible value does not require any memory to represent. In Zig: const Foo = enum {A}; the
Making full use of the type system can result in less additions to the language making things simpler overall. Knowing these things, the |
By the way, here's an implementation of oneValueType with current zig: fn OneValueType(comptime value: var) type {
const TheEnum = enum { OneValue };
return union(TheEnum) {
OneValue: @typeOf(value),
};
} However, this doesn't work since any argument you pass to |
And another way you could define the fn foo() enum {} {...} |
Is there a reason why 'comptime' is used to qualify b instead of 'const'? I feel I'm missing a nuance here. Consider that Default Field Values are implicitly comp-time known, and this proposal seems to add constness semantics on-top of DFVs. |
I don't understand why a new type or field type is needed. I think it would be better to have try @comptimeLoad(struct, name); Is it possible? |
A comptime struct field requires a default initialization value. Loads from a comptime struct field result in a comptime value of the default initialization value. Stores to a comptime struct field assert that the stored value is the default initialization value.
Generally, one should use a global const instead of a comptime field. The reason for using a comptime field is when you want reflection over struct fields to find the data as a field. For example:
This will construct an anonymous struct with all comptime fields and pass it to
dump
. Each iteration in the for loop will evaluate the@field(...)
expression and produce a comptime value.This is a prerequisite to #208 which is already accepted.
The text was updated successfully, but these errors were encountered: