#“expected field initializer” for struct

1 messages · Page 1 of 1 (latest)

celest sedge
#

Hello,

I'm currently translating some C code to Zig by hand, and I wanted to make one of the structure more “Zig-like” by adding an init function.

However, I'm having an “expected field initializer” error that I don't understand how to fix (on lines .qline[] and .vx[] .vy[]).

const qix_line_count: i32 = 20;

const QixLine = struct { x: [2]i32, y: [2]i32, color: u16 };

const Qix = struct { qline: [qix_line_count]QixLine, vx: [2]i32, vy: [2]i32, head: i32, tail: i32, initialized: i32, color_angle: i32,

pub fn init() Qix {
    return Qix {
    .head = 0,
    .tail = qix_line_count - 1,
    .qline[0].x[0] = random_num(k_screen_width),
    .qline[0].y[0] = random_num(k_screen_height),
    .qline[0].x[1] = random_num(k_screen_width),
    .qline[0].y[1] = random_num(k_screen_width),
    .qline[0].color = 0,
    .color_angle = 0,
    .vx[0] = random_num(17) - 8,
    .vy[0] = random_num(17) - 8,
    .vx[1] = random_num(17) - 8,
    .vy[1] = random_num(17) - 8,
    .initialized = 1,
    };
  }
};

Thank you for your time and answers!

crystal forum
#

you cant initalize an array like that, youd need to do something like .qline = [_]QixLine{ .{ .x = …, .y = …, .color = … }, … },

trail violet
#

i dont mean this as an insult in any way but ive seen other people think that this works before and im not sure why exactly. do other languages allow this? which one(s)?

celest sedge
crystal forum
trail violet
#

oh i didnt realize C allowed that

#

speaking of RLS, should change it to return .{ some semantics changed recently so T{} doesnt perform RLS

celest sedge
#

Thank you for your answers. I'm not looking to have the same behavior as C, but rather to do things “the Zig way”, because I think that I'm not learning Zig properly if I just transpose C code into Zig.