Zig NEWS

loading...
Cover image for Cool Zig Patterns - Generic over array length

Cool Zig Patterns - Generic over array length

Felix "xq" Queißner
・1 min read

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

Discussion (0)