src/main.zig:11:59: error: expected type '*T', found '*const T'
defer if (numbers_future.cancel(io)) |numbers| numbers.deinit(gpa) else |_| {};
~~~~~~~^~~~~~~
src/main.zig:11:59: note: T = array_list.Aligned(i32,null)
src/main.zig:11:59: note: cast discards const qualifier
If I understand the Zig 0.16.0 changelog correctly this is the recommended pattern to handle resources.
Use this pattern to avoid resource leaks and handle Cancelation gracefully:
But it fails because the std.ArrayList.deinit function takes the ArrayList by a mutable pointer which seems to be very common in Zig for deinit functions.
Am I missing something? How to correctly release resources in this case?
returned temporaries, even if captured in this way, are const. you must open a block, assign to a variable and then deinit. assuming the object is an ArrayList, this is safe to do, since the reason the function accepts *T is to do list.* = undefined, which is desirable in general to help catch use after free.
Temporary var is a good solution in general. In this case however, I’d probably just use toOwnedSlice() in the return from create numbers, and just have the slice in main, which doesn’t have this problem. In general prefer slices over arraylists if I’m done adding or removing values from it.