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

Enhance Visibility Control with @private and @public Annotations in Dart #4189

Open
stan-at-work opened this issue Dec 2, 2024 · 0 comments
Labels
feature Proposed language feature that solves one or more problems

Comments

@stan-at-work
Copy link

It would be nice if we could annotate a normal property with a @private or @public annotation to declare its visibility.

This way, we wouldn't have to struggle with the, in my opinion, impractical underscore convention for visibility. While it’s a clever approach, it’s not very practical.

Example: Current Dart Visibility Convention

In Dart, you use an underscore _ to mark a property as private within the library:

class Example {
  String publicProperty = "I am public";
  String _privateProperty = "I am private";

  void display() {
    print(publicProperty); // Accessible
    print(_privateProperty); // Accessible
  }
}

However, this convention relies on naming rather than explicit annotations, which can be error-prone or less readable.


Proposal: Use Annotations for Visibility

Imagine if Dart allowed visibility annotations, making code more explicit and standardized:

class Example {
  @public
  String publicProperty = "I am public";

  @private
  String privateProperty = "I am private";

  void display() {
    print(publicProperty); // Accessible
    print(privateProperty); // Accessible
  }
}

With annotations, changing visibility would be cleaner and would not require renaming properties. For instance, switching privateProperty to public would be as simple as changing the annotation:

@public
String privateProperty = "I am now public";

This approach could make visibility declarations more robust and readable, reducing potential naming conflicts and confusion. It would also integrate better with modern language features like metadata or decorators.
If Dart supported @private and @public annotations for visibility, a couple of issues with the current implementation would completely disappear.

@stan-at-work stan-at-work added the feature Proposed language feature that solves one or more problems label Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems
Projects
None yet
Development

No branches or pull requests

1 participant