-
Notifications
You must be signed in to change notification settings - Fork 205
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
Do implementers have to implement private members? #1006
Comments
The dart docs says so yes:
(https://dart.dev/guides/language/language-tour#implicit-interfaces) Note "visible only in this library", is you do |
Thanks. Those details are very important to keep in mind |
This approach doesn't seem safe to me. Consider: library a;
import 'b.dart';
void main() {
B()._thisIsPrivate; // NoSuchMethodError: Class 'B' has no instance getter '_thisIsPrivate'.
}
abstract class A {
int thisIsPublic;
int _thisIsPrivate;
} library b;
import 'a.dart';
class B implements A {
@override
int thisIsPublic;
} Now is too late to change this, but just for discussion matters, why not demanding implementations from different libraries to implement private members? Of course it would prevent implementations of classes with private members, but isn't safety more important? |
@wytesk133 wrote:
Yes, the basic rule is that it is an error if a concrete subtype does not have an implementation of a member, private or public. However, Dart used to be a much more dynamically checked language than it is today, and the treatment of missing private members across different libraries is a corner of the language where sound static checking has not (yet) been introduced, so there is indeed a potential for an error at run time which is not flagged at compile-time, as shown in @hugocbpassos' example here. It wouldn't be difficult at all to flag this error at compile-time, but it is a breaking change, which is the main reason why it hasn't been done. It would actually be a very good candidate for a lint, cf. dart-lang/sdk#58179. |
@lrhn pointed out in dart-lang/sdk#57805:
I'm curious how has this not been a bigger issue yet? I'd definitely be supportive of at least a lint to catch this. |
The behavior has been consistent since the dawn of Dart (at last since the removal of What likely happens is that classes are either intended to be implemented or not. If they are, the author won't write the unsafe |
This issue can also be fixed by disallowing private members to be accessed outside of the class that declares them (#2918), but this looks like a much less breaking change. |
That won't fix the problem for: class Optimist {
final int _value;
Optimist(this.value);
Optimist operator+(Optimist other) => Optimist(_value, other._value);
} The The issue is not the location, the issue is that |
Wow, that was fast. Guess this issue is still needed then since it would force any implementer of What if that proposal did restrict access to anything other than a self-access ( |
The core of the issue here is that in the following code, class A { }
// ------ Different libraries -------
class B extends A { }
class C implements A { } The reason for this is that void test(A a) { /* Use some private members of A */ }
void main() {
test(A()); // OK
test(B()); // OK
test(C()); // Compiles, but can lead to runtime errors
} If either this proposal or dart-lang/linter#2918 is accepted, then there will no longer be any difference. It will break some (inherently unsafe) code, but will ultimately lead to safer usage. |
On DartPad, the following code does not compile
The error is
The non-abstract class 'B' is missing implementations for these members: - A._thisIsPrivate
.Do implementers have to implement private members?
The text was updated successfully, but these errors were encountered: