Search code examples
listarraylistzig

Type of ArrayList in Zig


If I need to use zig's Arraylist as an argument of function or part of structure I need to know type of ArrayList I want to use. I used this way from some guide to create my list:

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();

    var list = std.ArrayList(usize).init(gpa.allocator());
    defer list.deinit();
}

and I already have many questions:

  • Which allocator should I use and do I realy need a variable for this?
  • What does defer do?
  • Why do I pass equation for unused variable gpa and then I use it?
  • Why do I deinit() variables that im going to use, and then I use them as usual? Isn't it free() from C?

Anyways, I thought if it works its fine, but then I realise I have to know the type:

std.debug.print("{}", .{@TypeOf(list)});

C:\Users\pasha\Desktop\gam>zig run s.zig
array_list.ArrayListAligned(usize,null)

But there's no array_list type. I searched and had notice that this function returns an unnamed struct:

pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
    if (alignment) |a| {
        if (a == @alignOf(T)) {
            return ArrayListAligned(T, null);
        }
    }
    return struct {
        const Self = @This();
        /// Contents of the list. Pointers to elements in this slice are
        /// **invalid after resizing operations** on the ArrayList unless the
        /// operation explicitly either: (1) states otherwise or (2) lists the
        /// invalidated pointers.
        ///
        /// The allocator used determines how element pointers are
        /// invalidated, so the behavior may vary between lists. To avoid
        /// illegal behavior, take into account the above paragraph plus the
        /// explicit statements given in each method.
        items: Slice,
        /// How many T values this list can hold without allocating
        /// additional memory.
        capacity: usize,
        allocator: Allocator,

        . . .
  • Whats type of unnamed struct, if I need this, or if not what do I even need?

Being more specific, I want to code Tetris game and need code like this:

pub const Tile = struct {
    x: u8,
    y: u8,
    color: rl.Color,

    pub fn TouchingGroundOrTile(self: *Tile, field: *const arralist of Tile) bool {
        for (field.*.items) |*tile|
            //if (it touches)
                return true;
        return false;
    }
};

But more importantly, I need to declare a pub variable as List of Tile like this:

pub var field: no_clue_what_the_type_is = undefined;

To be able to initialize() it later and use from another .zig file.

  • Do I think correct way or theres some true zig's way of doing this?

Solution

  • I was more attentive this time. I got notice that functions might be a type if they return type, I believe:

    pub fn main() !void {
        var gpa = std.heap.GeneralPurposeAllocator(.{}){};
        defer _ = gpa.deinit();
    
        var list: std.ArrayListAligned(usize, null) = undefined; //it works!!!
        list = std.ArrayList(usize).init(gpa.allocator());
        defer list.deinit();
    
        std.debug.print("{}", .{@TypeOf(list)});
    }
    

    Also I've learned what defer does (thanks @sigod's comment), so not need being helped from now.