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 private names as named record field and parameter names. #4251

Open
lrhn opened this issue Feb 2, 2025 · 1 comment
Open

Allow private names as named record field and parameter names. #4251

lrhn opened this issue Feb 2, 2025 · 1 comment
Labels
feature Proposed language feature that solves one or more problems

Comments

@lrhn
Copy link
Member

lrhn commented Feb 2, 2025

I was considering the arguments against allowing named parameters and record fields to have library private names, and I'm not sure they're that strong.

If a record type has a privately named field, then it has a type and shape that nobody outside of the library can express (unless you give them a public type alias for the shape). They might be able to get access to the type by inference, but they can't use it for much.
That's annoying and hard to work with, but not much different from any other private class name that isn't exposed, with a private getter name. Effectively the type of a record containing a library private name is a library private type, with a library private getter and no public constructor, which is all something you can already do with classes.

For named parameters, the same issue applies: It makes they type of the value inexpressible outside of the library, and makes it impossible to invoke the function if the named parameter is required. You can do a similar thing with a private class type and a private member name, or with a a function type that has private type as argument. You cannot create an instance of, or of a super type of, void Function(_PrivateType) either, and you can't invoke it unless you already have an instance of that private type. If you have an interface with a method with a private name, nobody can implement it (except maybe with noSuchMethod), where they could implement void Function(_PrivateType) by using a subtype like void Function(Object?). But then you could also just make the class base, and then nobody can implement it, so it's not really a feature that it can prevent implementation.

Which means that if we do allow privately named parameters and record fields, it'll prevent doing some things with such values and types, but not things that are significantly different from what it prevents to have private types or members otherwise.
That's not necessarily a deciding argument for why it can't be done.

(Not sure I want private members, but I am doing things with record wrappers and extension types that could use a little extra encapsulation.)

@lrhn lrhn added the feature Proposed language feature that solves one or more problems label Feb 2, 2025
@rrousselGit
Copy link

rrousselGit commented Feb 2, 2025

Agreed. I've wanted many times the ability to have private named parameters, in part for {this._named} instead of {@internal Type named}): _named = named

For starter, we can do:

class Foo {
  factory Foo() = _FooImpl;
  void method();
}

class _FooImpl implements Foo {
  @override
  void method({int? named});
}

That FooImpl.method.named parameter is for all intent and purposes private.

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

2 participants