#Does Zig have a mechanism for adding methods conditionally?

1 messages · Page 1 of 1 (latest)

mighty sluice
#

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?

spring fable
violet salmon
#

no, but you can have a comptime condition and have unreachable there to make the function fails to compile

spring fable
#

^

violet salmon
#

or @compilerError like Jodi mentions for message

mighty sluice
#

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.

violet salmon
#

yeah, just don't do mixin since Zig doesn't like it

spring fable
#

either works but itll probably be annoying to convert back and forth

violet salmon
#

but if you can provide a more concrete example, maybe we can suggest something

spring fable
#

Id just do it like youre doing rn and not worry about it exposing extra methods