Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pub attribute for struct fields #224

Open
katat opened this issue Nov 8, 2024 · 2 comments · May be fixed by #232
Open

Allow pub attribute for struct fields #224

katat opened this issue Nov 8, 2024 · 2 comments · May be fixed by #232
Labels

Comments

@katat
Copy link
Collaborator

katat commented Nov 8, 2024

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 katat added the easy label Nov 8, 2024
@bufferhe4d
Copy link
Contributor

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.

@mimoo
Copy link
Contributor

mimoo commented Nov 12, 2024

yes, and sg!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants