i'm currently looking at porting a project that requires me to use a ton of functions with the c calling convention from odin to zig, and in odin there's a feature that allows you to set the default calling convention for all the functions in a given file unless explicitly stated otherwise. i'm a pretty big fan of this feature and am hoping there's an equivalent in zig 0.12
#is it possible to set a default calling convention in a file
1 messages · Page 1 of 1 (latest)
https://github.com/free-audio/clap/blob/main/src/plugin-template.c for context, my odin project is a clap plugin and i'm currently trying to port this as an exercise in getting used to zig
GitHub
Audio Plugin API. Contribute to free-audio/clap development by creating an account on GitHub.
don't believe so
and I doubt it would be accepted if proposed
allowing the calling convention of a function to depend on the context in which it is declared, and thus requiring the reader to look at some separate part of the file, or a build configuration flag, is pretty antithetical to zig's stated goals
will note, if all you're using is the C calling convention, simply exporting or declaring a function extern will issue an error if the calling convention isn't specified - or well, that's the plan. Currently I believe it just defaults it to the C calling convention, but either way as long as your use case involves linking with the C ABI, it shouldn't suffer any accidental ABI mismatches because of an accidentally missing callconv(.C)
if we're talking callbacks which aren't exported nor extern, then pretty outta luck there, though you'll still get an error trying to pass a non-callconv(.C) function pointer where one is expected, so should still be a non-issue
The callconv is just another enum literal so you can do something like:
const cc = .C;
// ...
fn foo() callconv(cc) void {}
fn bar() callconv(cc) void {}
Althought I guess this doesn't save you much typing for the .C case
true, can do that if you're worried about mismatching it or having to change it at some point
oh crap i thought i'd get notifications for this
reading now
ah that's a bummer
at least the reasoning behind it makes sense
i'd thought about this but the calling convention isn't going to change to it's not that useful