#What's wrong with the way I am trying to initialize this matrix?

1 messages · Page 1 of 1 (latest)

celest scroll
#
const Matrix = struct {
    buffer: [3][3]f64,

    pub fn translate(dx: f64, dy: f64) Matrix {
        return Matrix{
            .buffer = [
                [1.0, 0.0, dx],
                [0.0, 1.0, dy],
                [0.0, 0.0, 1.0],
            ],
        };
    }
};
fluid fiber
#

that's not how you initialise an array. use .{} syntax instead

#

so:

return Matrix{
    .buffer = .{
        .{ 1.0, 0.0, dx },
        .{ 0.0, 1.0, dy },
        .{ 0.0, 0.0, 1.0 },
    },
};
celest scroll
#

thank you kind sir

fluid fiber
#

note that this initialisation syntax (.{}) is not specific to arrays, and also works for structs and unions.
it's basically saying "I am creating a value which has this internal structure. Zig, please find out which type I'm talking about".

if you do want to be explicit with the arrays' types you can write it like so:

return Matrix{
    .buffer = [3][3]f64{
        [3]f64{ 1.0, 0.0, dx },
        [3]f64{ 0.0, 1.0, dy },
        [3]f64{ 0.0, 0.0, 1.0 },
    },
};

replacing the . with the type's name