I’m working on a template language that compiles down to Zig, and it would help me a LOT if I could have unused function parameters without explicitly discarding them. I assume there is no way to do that, but I thought I’d ask anyway and maybe I’ll be surprised.
I’d let the others answer directly, but I can suggest to use anytype and pass void, or an optional with null as a workaround.
No, this is not possible.
However if you want to transpile and don’t care about whether the result is readable, you could always discard all function parameters with _ = ¶m_name;, it is ugly, but at least you could compile the code until you figure out a better solution.
If you use the ZLS VSCode extension, it has an option that automatically adds and removes the discards based on usage. Would be nice to have equivalent functionality in other code editors as well.
Thanks, it didn’t occur to me I could fool the compiler like this to accept “useless” discards. The thing is, for a proper fix, I need to do liveness analysis on the Zig code inside the template that I currently treat as opaque. I really wanted to avoid having to understand the Zig code, so discarding all params like this helps me a lot.
You have to discard them. A Few tips you can use:
-
you can discard in the function declaration:
fn foo(_: i32) {} discards the integer. -
you can discard many things with a tuple
fn foo(a: i32, b: i32, c: i32) void {
_ = .{a, b, c};
}
As someone who regularly watch videos about air crash investigations, I found this arrangement completely insane. Imagine if the ground proximity warning system of a passenger jet works in manner. It’d go off whenever the radar return indicates the plane is near the ground. During landing, pilots would be greeted by continual warnings of “TERRAN AHEAD! PULL UP!”. And the same when their plane is just standing in line waiting for take-off. How would pilots react to such an asinine system? Now imagine that they’re offered, as a solution, a voice-activated robot arm that automatically pushes the mute button.
You just described why zig has this error and why you can get around it explicitly.
The zls feature is off by default, and I don’t think anyone who understands the utility of this language design would ever turn it on.
But not everyone is writing high stakes software, I don’t see why they shouldn’t be allowed this convenience.
And I would expect anyone who is writing high stakes software to know better than to use such a feature.