Closed

Description
Imagine a struct instance with fields that must stay internally consistent. Then it would be helpful to be able to mark these fields as "read only" when referenced outside their own "class/namespace/struct".
// status quo:
const DelicateState = struct{
x : u8,
y : u8, // y must be 60% of z to closest integer - x/2 to closest integer
z : u8, // z must always be double of x mod 256!
}
// want
const Delicate = struct{
//"selfmut" marks a variable as mutable only within its own struct/namespace
x : selfmut u8,
y : selfmut u8,
z : selfmut u8,
fn modifyX(self: Delicate, x : u8){
// set x
// modify y and z so they are consistent
}
fn modifyY ....
fn modifyZ ....
}
test "Delicate struct" {
const dct = Delicate.init();
dct.x = 43; // -> compile time error!
dct.modifyX(43); // -> OK!
_ = dct.x; // -> OK!
}
This feature is arguably just about helping developers discipline themselves. So it might be out of scope for a low-level oriented language like zig. Though at the same time, this kind of feature can be very helpful for maintainability in larger projects, if that is within zig's scope.