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

private struct fields #620

Merged
merged 1 commit into from
Jan 6, 2022

Conversation

cburgdorf
Copy link
Collaborator

@cburgdorf cburgdorf commented Jan 3, 2022

What was wrong?

As described in #236 we need support for private fields in structs.

How was it fixed?

Public fields now need to be declared with the pub modifier, otherwise they default to private fields.
If a struct contains private fields it can not be constructed directly except from within the
struct itself. The recommended way is to implement a method new(...) as demonstrated in the
following example.

struct House:
    pub price: u256
    pub size: u256
    vacant: bool

    pub fn new(price: u256, size: u256) -> House
      return House(price=price, size=size, vacant=true)

contract Manager:

  house: House

  pub fn create_house(price: u256, size: u256):
    self.house = House::new(price, size)
    let can_access_price: u256 = self.house.price
    # can not access `self.house.vacant` because the field is private

@cburgdorf cburgdorf changed the title WIP: private struct fields private struct fields Jan 4, 2022
@cburgdorf cburgdorf marked this pull request as ready for review January 4, 2022 13:30
@cburgdorf cburgdorf requested a review from sbillig January 4, 2022 13:32
Copy link
Collaborator

@sbillig sbillig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! 🤠

Comment on lines 625 to 632
Err(FatalError::new(scope.fancy_error(
&format!(
"Can not access private field `{}` on struct `{}`",
&field.kind, struct_.name
),
vec![Label::primary(field.span, "private field")],
vec![],
)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't have to be fatal.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eagle eyes! 🦅

@cburgdorf cburgdorf merged commit 07c610a into ethereum:master Jan 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants