-
-
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
New keyword "field" for struct/enum instance variables/functions. #2859
Comments
"Explicit" might be the wrong term to describe the goal here. I think what your proposal perhaps offers is more immediate readability. In my opinion that's up for debate, as the mandatory presence of |
I do think this would look better at top level scope. One downside is it makes anonymous structs more tedious. |
I don't think it's very difficult to see the difference between instance members and namespace members either, but this "explicitness" might make it easier to add modifiers to fields later on, e.g that
I see your point. Here's some ideas (of varying quality) on alternative names: |
Slightly related, this could also allow a more unified declaration syntax with regard to commas and semicolons: Static members fields are terminated by ; and non-static member fields by ,. I believe that adds a bit of confusion, and having a different way of stating 'static-ness' could also enable an alternative syntax where every struct member is terminated in the same way. |
How about a keyword to document static members instead? That style is pretty common from the Java syntax family and it keeps most of the noise down: const S = struct {
x : i32,
y : i32,
static var z: i32 = 0,
static const t: i32 = 5, I really don't like the |
I'm in favor of not polluting non-static members with a designator, that would just make it feel more verbose. |
Unifying struct and function syntax could also be considered. const Example = struct( ...instance members go here... ){... namespace members go here...}
const S = struct(x: i32, y: i32){ fn add(self: S, other: S){} }
const E = enum(ON, OFF, UNKNOWN){}
const U = union(ERROR: MyError, Result: MyPayload){}
const F = fn(x: i32, y: i32){} // functions already "look" like this. Parenthesis behind the enum and union keywords are already used for something though. Ordinal values for enums and making tagged unions. And for structs with a lot of instance members, you would need two multi-line scopes for one struct. |
I know you're not necessarily amenable to this idea, but if top-level readability is a concern, and since every zig file is a struct, you might consider forcing every file's contents to be in an anonymous struct expression, i.e.: // can't write code here
struct {
... // your code here
} // <-- no semicolon because this is an expression, not a statement
|
Making an executive decision to stick with status quo on this one. |
In Zig, the difference between "namespace members" of a struct (or other container like enum) and "instance members" could be more explicit. Compare with Java, where "namespace members" would be designated with the static keyword.
It's not much of a problem right now, but if later on Zig will add more access modifiers to fields, then it could be helpful when you see
const field x: i32
, to know that it is different fromconst x: i32
, or fromfield x: i32
. Here,const field
would open up the possibility to mark an instance member as immutable.The text was updated successfully, but these errors were encountered: