Skip to content

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

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

Closed
stan-at-work opened this issue Dec 2, 2024 · 2 comments
Closed
Labels
feature Proposed language feature that solves one or more problems state-duplicate This issue or pull request already exists

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
@TekExplorer
Copy link

This is strictly worse than what we have now.

The underscore is actually rather readable. You see an underscore? Its private. No need to check what annotation it has. No need to go to the original declaration in code review to check.

That said, I do think that adding this wouldn't be bad, especially for cases like UnmodifiableList which may want to hint to the analyzer that the modification functions should be hidden from auto complete and warned against if it's used anyway.

Granted, that may just be @internal or some other existing annotation, but those are my 2¢

@lrhn
Copy link
Member

lrhn commented Mar 11, 2025

It's not a new idea to add declared public, private and or protected access modifiers, rather than only having library privacy.
It has generally not been considered a good design choice for Dart, and not a real improvement over library privacy.
The only think library privacy can't do is protected access.

One of the advantages of having different names for private members is that it avoids naming conflicts.
If you use access modifiers, you may not need to to rename variables when going from public to private,
but you risk the private member having the same name as a public member from somewhere else.
Privacy is both about controlling accessibility and about avoiding name conflict between things you can access and things you can't. Because you shoulnd't need to know about the latter at all.

Existing issues include:

And other approaches to the same issue, like.

Using annotations would not be a language feature. Annotations won't be able to change the meaning of a program.
So any language feature won't be using that syntax.

@lrhn lrhn closed this as completed Mar 11, 2025
@lrhn lrhn closed this as completed Mar 11, 2025
@lrhn lrhn added the state-duplicate This issue or pull request already exists label Mar 11, 2025
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 state-duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants