#Creating a constant array of structs, containing arrays

1 messages · Page 1 of 1 (latest)

spring jay
#

Hi, I'm wanting to create an array of this object at comptime

const Flag = struct {
    name: []const u8,
    aliases: []const []const u8,
    used_in: []Cmd,
    enabled: bool = false,
};

I have this so far:

const Flags = [_]Flag{
    Flag{
        .name = "installed",
        .aliases = &[_][]const u8{ "-i", "--installed" },
        .used_in = &[_]Cmd{.help},
    },
    Flag{
        .name = "reload_cache",
        .aliases = &[_][]const u8{ "-rc", "--reload-cache" },
        .used_in = &[_]Cmd{ .list, .install },
    },
};

But I'm getting a compiler error, telling me it expected type '[]Cmd', found '*const [1]Cmd'
(Cmd is just an enum)

How might I get around this?

acoustic condor
#

Also just fyi you can use &.{ ... } instead of &[_][]const u8{ ... } and &[_]Cmd{ ... }, it'll infer the type

#

Same for Flag{ ... }, that can just be .{ ... }

spring jay
#

Ah - that's done the job. Thanks :)