Zig NEWS

Cover image for Cool Zig Patterns - Generic over array length
Felix "xq" Queißner
Felix "xq" Queißner

Posted on

Cool Zig Patterns - Generic over array length

This might come handy when you need to work with arrays, not slices:

fn myFunc(array: anytype) void {
  const real_array: [array.len]u32 = array;
  return myFuncSlice(&real_array); 
}
fn myFuncSlice(slice: []const u32) void {
  ...
}

test "from array" {
  var arr: [3]u32 = .{ 1, 2, 3};
  myFunc(arr);
}

test "from anonymous literal" {
  myFunc(.{ 1,2,3,4 });
}


Enter fullscreen mode Exit fullscreen mode

Latest comments (0)