Let's say I have a generic struct fn Image(comptime PIX: type) type. There are some methods that I want to have on all images, e.g. fn get_pixel(self: Self, x: u32, y: u32). But some methods I only want to have on an Image(Rgba), for example fn get_alpha_mask(self: Self) -> Image(bool). Is there a way to say at comptime, only include a function in the struct namespace if some condition is true?
#Does Zig have a mechanism for adding methods conditionally?
1 messages · Page 1 of 1 (latest)
You cant conditionally include decls, but can make the decl noreturn or have the function be @compileError so it cant be used
no, but you can have a comptime condition and have unreachable there to make the function fails to compile
^
or @compilerError like Jodi mentions for message
Okay great. The other thing I thought about doing is having a wrapper struct RgbaImagearound Image(Rgba) that has some additional methods and uses Image(Rgba)as a mixin. But that is a bit difficult with usingnamespace being removed since 0.15.1.
yeah, just don't do mixin since Zig doesn't like it
either works but itll probably be annoying to convert back and forth
but if you can provide a more concrete example, maybe we can suggest something
Id just do it like youre doing rn and not worry about it exposing extra methods