#Optional generic struct value

1 messages · Page 1 of 1 (latest)

shrewd mica
#

If I make a generic, like a function that returns a struct type, can I make some version of the generic have a value in the steuxt and another not? Not just null the value, but completely omit it from the struct?
Example:

pub fn Image(comptime color_type: ColorType) type {
    return struct {
        width: u8,
        height: u8,
        if (color_type == .indexed) palette: []Color,
        pixels: [][]Color
    }
}

Where Image(.indexed) has the palette but Image(.rgb) does not?

pulsar vigil
#

you will stil be able to do .palette, but void is zero-size so it won't take up any space

shrewd mica
#

Ok

#

Thanks

pulsar vigil
#

and you'll have to initialize it to .palette = {} in the non-palette case

shrewd mica
#

When it's void?

pulsar vigil
#

yeah