#User struct using array initialization syntax

1 messages · Page 1 of 1 (latest)

normal vortex
#

I have a numerical struct const Version = struct { major: u8, minor: u8, patch: u8 } for which it is entirely readable (imo more readable) to use array initialization syntax, i.e. const v : Version = {1, 0, 0}.
Is this possible or must the fields be specified?

wary vessel
#

you have to give the field names for this or you could create a function

#

or make the struct a tuple but i wouldnt argue for that

normal vortex
#

Seeing as it would be just a syntax nicety I don't think I'll bother and just use the specifiers, thanks

cosmic trail
#

Most you can get is something like this```
const Version = struct {
major: u8,
minor: u8,
patch: u8,

fn init(major: u8, minor: u8, patch: u8) Version {
    return .{
        .major = major,
        .minor = minor,
        .patch = patch,
    };
}

}

const v: Version = .init(1, 0, 0);```

#

Imo it's best to just use the field names