4

I found some (a bit strange) feature of zig when played with function pointers.

Here is an example which I beleive is written as implied.

const std = @import("std");
const print = std.debug.print;

const aFunc = fn (x: i32) void;

fn theFunc(x: i32) void {
    print("have {}\n", .{x});
}

pub fn main() void {
    const f: aFunc = theFunc;
    f(3);
}

This code compiles and runs Ok.

Now change it like this.

const std = @import("std");
const print = std.debug.print;

const aFunc = fn someFunc (x: i32) void;

fn theFunc(x: i32) void {
    print("have {}\n", .{x});
}

pub fn main() void {
    const f: aFunc = theFunc;
    f(3);
}

This code is also Ok, but shouldn't the compiler emit error/warning about someFunc? This name is useless - when I tried to use it instead of aFunc there was an compilation error.

Compiler version:

$ /opt/zig/zig version
0.10.0-dev.3431+4a4f3c50c

It looks like the zig source parser treats

const aFunc = fn someFunc (x: i32) void;

as if it is function definition, but silently drops someFunc.

dee0xeed
  • 61
  • 5

0 Answers0