#mutex discard const qualifier

1 messages · Page 1 of 1 (latest)

dry shoal
#

I have something like

mutex: std.Thread.Mutex = .{},

pub fn init() Self {
    return .{};
}

pub fn f(self: Self) void {
    self.mutex.lock();
    defer self.mutex.unlock();
    // ...
}

And I'm getting

error: expected type '*Thread.Mutex', found '*const Thread.Mutex'
        self.mutex.lock();
        ~~~~~~~~~~^~~~~
note: cast discards const qualifier

Huh? It's a mutable field on self. There's no explicit const pointers in my code. Why is zig choosing to convert self.mutex to a *const and then complaining about it?

modest widget
#

*Self in the argument

#

because arguments are by value in zig, they are immutable

dry shoal
#

ohhh of course.

#

thanks