#initialize array with arrays

1 messages · Page 1 of 1 (latest)

dense spear
#

I have an array of type [_]Data_t and i initialize them with
[2]Data_t
[3]Data_t
or just Data_t

the variations is just lumpy functions.

doThing1(...) [3]Data_t

so i just want to initialize the array from smaller arrays.

const DO_BIG_THING = [_]Data_t{
  doThing1() ++
  doThing2() ++
  Data_t{}
}

seems to bark at me.

prisma wolf
#

you probably want something like```ts
const do_big_thing = doThing1() ++ doThing2() ++ .{Data_t{}};

dense spear
#

It keeps not like it.

error: expected type 'Data_t', found '[5]Data_t'
        }) ++
           ^~
#

right at the end when appending to the last thingy.

prisma wolf
#

please do share more of the surrounding code, as well as relevent functions' signatures

dense spear
#
const Data_t = u8;
const std = @import("std");

pub fn doThing1() [2]Data_t {
    return .{ 1, 2 };
}

var OPERATIONS = [_]Data_t{
    .{} ++ doThing1(),
};

test "thing" {
    for (OPERATIONS) |i| {
        std.debug.print("{i} ", .{i});
    }
}
prisma wolf
#

I think you misunderstand the meaning of the ++ operator. as the two operands, it takes arrays of a certain type, and then outputs a concatenation of the arrays (the same can be done with pointers-to-arrays, or slices, to get a pointer-to-array or slice result resp.)

the expression .{} ++ doThing1() then already gives you an array of Data_ts (I'm not sure if Zig copes with the .{} over there, but that's besides the pointer) - you're then trying to use that Data_t... as an element of a Data_t array!

dense spear
#

I see the errors of my way.