Skip to content
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

Question on distinction between access of fields and namespaced declarations #8015

Closed
yohannd1 opened this issue Feb 14, 2021 · 3 comments
Closed
Milestone

Comments

@yohannd1
Copy link

yohannd1 commented Feb 14, 2021

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":

const S = struct {
    field1: u8,
    field2: i32,
    
    const some_info = 50;
};

pub fn main() void {
    _ = S.some_info; // "static declaration" access

    const s = S{ .field1 = 10, .field2 = 20 };
    _ = s.field1; // field access
}

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.

struct S {
    char field1;
    int field2;

    static const int some_info = 50;
};

int main() {
    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.

@kyle-github
Copy link

Related: #8012.

There is a lot of discussion in that issue that you might find useful.

@ghost
Copy link

ghost commented Feb 14, 2021

@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.

@yohannd1
Copy link
Author

yohannd1 commented Feb 14, 2021

@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.

@andrewrk andrewrk added this to the 0.8.0 milestone Jun 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants