Skip to content

struct method with name same as field is impossible to call #13012

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

Closed
wooster0 opened this issue Sep 29, 2022 · 1 comment
Closed

struct method with name same as field is impossible to call #13012

wooster0 opened this issue Sep 29, 2022 · 1 comment
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@wooster0
Copy link
Contributor

wooster0 commented Sep 29, 2022

Zig Version

0.10.0-dev.4176+6d7b0690a

Steps to Reproduce

const X = struct {
    y: void = {},

    fn y(x: @This()) void {
        _ = x;
    }
};

test {
    const x = X{};
    _ = x.y;
    _ = x.y();
}

Expected Behavior

$ zig test x.zig
./x.zig:4:5: error: method cannot be called because of field with same name
    fn y(x: @This()) void {
    ^~~~~~~~~~~~~~~~~~~~~~~

Actual Behavior

$ zig test x.zig
x.zig:12:10: error: type 'void' not a function
    _ = x.y();
        ~^~

So this is a bit awkward. I don't know if maybe status quo is actually fine here but I don't think it is because this is ambiguous: if I do x.y() here, am I trying to call the struct's field y because I think it is *const fn(...) X or do I want to call the method fn y(x: @This()) void? I think it should simply be an error to define this method like that because it does seem to be impossible to actually call the method I defined.

Also, this error is also simply confusing when in your head you intended to call the method but the error talks about some field that you don't have in your head, so I think that's another reason to do a change here.

Also I have the suspicion this might be a duplicate so I apologize for that in advance.

@wooster0 wooster0 added the bug Observed behavior contradicts documented or intended behavior label Sep 29, 2022
@Vexu
Copy link
Member

Vexu commented Sep 29, 2022

This is covered by #705 and #7942

@Vexu Vexu closed this as completed Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants