You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
On Zig you can put both field declarations and what I'd call "static declarations", such as functions, vars and consts inside structs. The acess of both a field and "static declarations" are done by the . (dot) "operator":
This is different to the way C++ does things (this specific example being compiled with gcc -std=c++20), where there's a distinction between both kinds of acesses: :: is used for static declarations and . is used for fields.
structS {
char field1;
int field2;
staticconstint some_info = 50;
};
intmain() {
auto _ = S::some_info; // "static declaration" access
S s = S { .field1 = 10, .field2 = 20 };
auto _2 = s.field1; // field access
}
In my opinion C++'s way of doing this communicates intent more precisely (which is listed on the Zen section of the documentation), since it uses different operators to do these two different operations.
We could go even further and consider calls to methods that take a self (e.g. fn method(self: *@This()) void {}) to be yet another operation and add a separate operator for it too, but I'm not sure if that would be too far to the point of being inconvenient.
Has there been any discussion on the matter? I tried searching about it but couldn't find anything relevant.
Note: I didn't make this into a proposal because I don't think I have much information on the matter (hence the question), and I don't have a good idea on how to implement this without potentially breaking a lot of code yet.
The text was updated successfully, but these errors were encountered:
@YohananDiamond, I think C++'s :: namespace operator is an exception rather than the rule among modern languages. E.g. D and Java, among many others, uniformly use the dot for accessing names within classes, instances and modules/packages. Zig has gone even further and removed the distinction between a struct and a module altogether (#1047, #2022). This was done in part due to Zig's push for minimalism, which is occasionally prioritized even over communicating intent precisely.
@zzyxyzz Although I like the :: operator quite a lot, I think I can agree with your point here. And not only that, it's a quite crucial part of the language and changing it on existing codebases will probably be even more inconvenient.
On Zig you can put both field declarations and what I'd call "static declarations", such as functions,
var
s andconst
s inside structs. The acess of both a field and "static declarations" are done by the.
(dot) "operator":This is different to the way C++ does things (this specific example being compiled with
gcc -std=c++20
), where there's a distinction between both kinds of acesses:::
is used for static declarations and.
is used for fields.In my opinion C++'s way of doing this communicates intent more precisely (which is listed on the Zen section of the documentation), since it uses different operators to do these two different operations.
We could go even further and consider calls to methods that take a
self
(e.g.fn method(self: *@This()) void {}
) to be yet another operation and add a separate operator for it too, but I'm not sure if that would be too far to the point of being inconvenient.Has there been any discussion on the matter? I tried searching about it but couldn't find anything relevant.
Note: I didn't make this into a proposal because I don't think I have much information on the matter (hence the question), and I don't have a good idea on how to implement this without potentially breaking a lot of code yet.
The text was updated successfully, but these errors were encountered: