ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
33.84k stars 2.48k forks source link

allow calling member function pointer on container value #18233

Open Techatrix opened 9 months ago

Techatrix commented 9 months ago

Zig Version

0.12.0-dev.1802+56deb5b05

Steps to Reproduce and Observed Behavior

fn someFunction(_: *const Foo) void {}
const Foo = struct {
    pub const func = &someFunction;
    //               ^ note the address of here
};

comptime {
    const foo = Foo{};
    _ = foo.func();
}

output:

crash.zig:9:12: error: no field or member function named 'func' in 'crash.Foo'
    _ = foo.func();
        ~~~^~~~~
crash.zig:2:13: note: struct declared here
const Foo = struct {
            ^~~~~~
crash.zig:3:9: note: 'func' is not a member function
    pub const func = &someFunction;
    ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~

Expected Behavior

compile successfully just like when calling a non pointer member function:

fn someFunction(_: *const Foo) void {}
const Foo = struct {
    pub const func = someFunction;
};

comptime {
    const foo = Foo{};
    _ = foo.func();
}
iacore commented 8 months ago

I met this problem while enhancing raylib-zig.

Update: is typo. The error message is the same.

pub const Vector3 = extern struct {
    x: f32,
    y: f32,
    z: f32,

    pub fn init(x: f32, y: f32, z: f32) Vector3 {
        return Vector3{ .x = x, .y = y, .z = z };
    }
    pub const add = rlm.vector2Add;
    pub const sub = rlm.vector2Subtract;
};
_ = p.add(q); // not member function
Techatrix commented 8 months ago

I met this problem while enhancing raylib-zig.

typo?

<     pub const add = rlm.vector2Add;
<     pub const sub = rlm.vector2Subtract;
>     pub const add = rlm.vector3Add;
>     pub const sub = rlm.vector3Subtract;
iacore commented 8 months ago

Yes, it is typo. Thanks!