#Hot to get a non-optional pointer to a optional field?

1 messages · Page 1 of 1 (latest)

fathom pelican
#

I have this struct

pub const U = struct {
  base_id: u32,
  u0: ?T = null,
  u1: ?T = null,
  u2: ?T = null,
  u3: ?T = null,
}

Based on input I want to check, if field is null, if it is, generate default, and in both cases return *T, is that even possible?
My current idea is something like this:

const un :*?T =
    switch (u) {
        0 => &entry.u0,
        1 => &entry.u1,
        2 => &entry.u2,
        3 => &entry.u3,
    };
if (un.*) |r| {
    return &r;
} else {
    
}

But can not get it to compile.

grizzled pawn
#

you can have something like this:```ts
if (container.field == null) container.field = generateValue();
const ptr = &(container.field orelse unrechable);