#It is possible to have optional anonymous struct literals?

1 messages · Page 1 of 1 (latest)

olive barn
#

For model configuration data I would like to know if it is possible to have something like
const a1 = .{ .b = null };
const a2 = .{ .b = ?.{ .c = 1 } };
to express that b is set or not. If b is set it might have more data present.
It is possible and what would be the syntax for this?

brave valve
#

Not really, you should just define a struct and then do const a1: MyConfigStruct = .{ ... };

#

You could also do it with an anonymous struct type: ```rs
const a1: struct {
b: ?struct {
c: u32,
},
} = .{ .b = .{ .c = 1 } };

ivory tendon
#

Yeah, the ? declares an optional type, but anon struct literals are values, not types. null on the other hand is a value, which is why a1 works.