You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.)
The text was updated successfully, but these errors were encountered:
lrhn
added
the
feature
Proposed language feature that solves one or more problems
label
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
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 withnoSuchMethod
), where they could implementvoid Function(_PrivateType)
by using a subtype likevoid Function(Object?)
. But then you could also just make the classbase
, 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.)
The text was updated successfully, but these errors were encountered: