Erik Engheim
Erik Engheim

Reputation: 8512

How do I mutate a Zig function argument?

I have discovered that Zig function parameters are constant. That means my naive function for freeing a HashMap doesn't work. You can see an example of the code here. I am wondering if the most correct Zig way is to pass dict as a function or if there is some other way in which I can make a parameter mutable.

const Dict = std.StringHashMap;

fn releaseDict(allocator: Allocator, dict:  Dict(i16)) void {
    var iter = dict.iterator();
    while (iter.next()) |entry|
        allocator.free(entry.key_ptr.*);
    dict.deinit();    
}

Upvotes: 10

Views: 4987

Answers (2)

aftamat4ik
aftamat4ik

Reputation: 718

like this:

/// pass pointer as arg, modify pointer, test for error
/// !i32 allows to pass as function result i32 numbers and error types
fn func_summ_error(a: *i32,  b: i32) !i32 {
    if (a.* < 0 or b < 0) return error.OutOfRange; // we can write anything after error. word, in here it's .OutOfRange
    std.debug.print("initial a = {d}\n", .{a.*});
    a.* = a.* + 10; // a will be mutated, .* - dereference pointer
    std.debug.print("pointer mutated a = {d}\n", .{a.*});
    return a.* + b;
}

var a: i32 = 2;
const b = 5;
// &a - getting address (pointer) from a
// since this function returns !i32 we can call it with 'try' to catch errors when they appear here
summ = try func_summ_error(&a, b);
std.debug.print("\na = {d}", .{a});

will print:

pointer mutated a = 12

a = 12

Upvotes: 0

sigod
sigod

Reputation: 6486

You don't. Function parameters are immutable by design:

Structs, unions, and arrays can sometimes be more efficiently passed as a reference, since a copy could be arbitrarily expensive depending on the size. When these types are passed as parameters, Zig may choose to copy and pass by value, or pass by reference, whichever way Zig decides will be faster. This is made possible, in part, by the fact that parameters are immutable.

Modifying function parameters can easily lead to unexpected results. If the parameter is passed by value (a copy of it is made), modifying it would not modify the original value.

What you want to do here is: pass a pointer to your hash map. E.g.

fn releaseDict(allocator: Allocator, dict: *std.StringHashMap(i16)) void {
    // ...
}

Upvotes: 11

Related Questions