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 setting get and set access levels separately #1416

Open
thislooksfun opened this issue Mar 16, 2023 · 1 comment
Open

Allow setting get and set access levels separately #1416

thislooksfun opened this issue Mar 16, 2023 · 1 comment

Comments

@thislooksfun
Copy link
Contributor

I was just watching the Toy DOM video and one of the complaints Andreas mentioned was needing to make some of the members public because they need to be read outside of the class. Explicit getters/setters are one way around this, but support isn't there yet (see ~15:35 in the aforementioned video), and also I personally find they clutter the code quite a bit.

Instead I would like to propose another feature copied from Swift: the ability to mark a member as being publically gettable, but only privately settable. The Swift syntax for this is private(set) (docs), but I've always found that syntax a bit confusing, so I'm open to other suggestions. Doing it this way allows the best of both worlds: the access of the member is a pure reference, so no extra tracking is needed, but only the class is allowed to modify the value.

Here is an example of the Text node from the start of the video ported to Swift:

class Text {
  private(set) var data: String

  init(data: String) {
    self.data = data
  }
}

let text = Text(data: "Hello, world!")
print(text.data) // "Hello, world!"
text.data = "Hello, world!" // Error: Cannot assign to property: 'data' is a private(set) property
@awesomekling
Copy link
Contributor

That sounds really cool! Agree that the private(set) syntax is a bit clunky.

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

No branches or pull requests

2 participants