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

expected type expression, found ',', when trying to put a method in anonymous struct literal type #18295

Open
baryluk opened this issue Dec 17, 2023 · 3 comments
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Milestone

Comments

@baryluk
Copy link

baryluk commented Dec 17, 2023

Zig Version

0.12.0-dev.1829+70664547c

Steps to Reproduce and Observed Output

pub fn FunctionWithCapture() type {
    return struct {
        function: fn() i32,
        pub fn call(self: *@This()) i32 {
            return self.function();
        },
    };
}

I just started playing today (coming from D, Go, C++, Python), and it was very confusing, when I got:

c.zig:6:10: error: expected type expression, found ','
        },
         ^

zig-linux-x86_64-0.12.0-dev.1829+70664547c

Expected Output

Should fail after initial (or actually just during) pub, due to reserved keyword used, or missing comma after pub, with appropriate message.

@baryluk baryluk added the error message This issue points out an error message that is unhelpful and should be improved. label Dec 17, 2023
@baryluk baryluk changed the title expected type expression, found ',', when trying to put a method in anonymous struct literal expected type expression, found ',', when trying to put a method in anonymous struct literal type Dec 17, 2023
@baryluk
Copy link
Author

baryluk commented Dec 17, 2023

Wait...

https://ziglang.org/documentation/master/#toc-struct

shows this is a correct code.

Modulo the trailing , which should not be used.

So, maybe message should be more like:

Expected identifier, function declaration, or '}', but found ','

?

@baryluk
Copy link
Author

baryluk commented Dec 17, 2023

Similar (but slightly unrelated):

pub fn FunctionWithCapture() type {
    return struct {
        function: fn() i32,
        pub fn call(self: *@This()) i32 {
            return self.function();
        }
    };
}

pub fn main() !void {
  const T = FunctionWithCapture();
  const t = T{
    .function = fn() i32 { return 5; },
  };
}

Got:

c.zig:13:36: error: expected ',' after initializer
    .function = fn() i32 { return 5; },
                                   ^

rather confusing.

I guess this is because of #1048 and in the docs I did not find a single occurence of = fn( , and due to #1717, so probably this is not something that is actually supported.

So error, does make some sense now, but it is not super obvious to a novice.

@xdBronch
Copy link
Contributor

declarations are not followed by commas, the fix is to just remove the trailing comma after the closing curly brace.

I guess this is because of 1048 and in the docs I did not find a single occurence of = fn( , and due to 1717, so probably this is not something that is actually supported.

right, the error message certainly needs some improvement but this is not meant to work

@Vexu Vexu added this to the 0.13.0 milestone Dec 17, 2023
@andrewrk andrewrk modified the milestones: 0.14.0, unplanned Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error message This issue points out an error message that is unhelpful and should be improved.
Projects
None yet
Development

No branches or pull requests

4 participants