noname icon indicating copy to clipboard operation
noname copied to clipboard

Allow pub attribute for struct fields

Open katat opened this issue 1 year ago • 2 comments

For struct fields, sometimes we want to control the accessibility of the fields, so they are only accessible within the struct methods.

For example:

struct Room {
	pub beds: Field, // public
        size: Field // private
}

Uint8.update_size(self, size: Field) {
        self.size = size; 
}

fn main() {
       let room = Room {beds: 2, size: 10};
       uint.beds = 2; // allowed
       uint.size = 5;  // not allowed
       uint.update_size(5); // allowed
}

When there is a pub attribute for a struct field, the field can be accessible from external. Otherwise, the private fields are only accessible in the struct methods.

katat avatar Nov 08 '24 04:11 katat

I guess the example should have Uint8 replaced with Room and uint with room right?

I can try to attempt this if no one else is already working on it.

bufferhe4d avatar Nov 11 '24 13:11 bufferhe4d

yes, and sg!

mimoo avatar Nov 12 '24 11:11 mimoo