Skip to content

Instantly share code, notes, and snippets.

@osa1
Last active January 26, 2023 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osa1/02affec198784ff759cf374623ea49f1 to your computer and use it in GitHub Desktop.
Save osa1/02affec198784ff759cf374623ea49f1 to your computer and use it in GitHub Desktop.
Strange Dart 9 (1)
// Private members are not part of a class's interface, but implementing
// classes need to implement them.
//
// ... except when the class being implemented is in a separate library. See
// the second part: https://gist.github.com/osa1/a6b70f36d1041996c3ff7fa12c7589d8
class A {
int _a = 1;
int _b = 2;
@override
String toString() => '$_a $_b';
}
// Compile-time error, even though this implements the public interface
// class B implements A {}
// Compile-time error, does not implement setters
// class C implements A {
// int get _a => 1;
// int get _b => 2;
// }
// OK
class D implements A {
int get _a => throw '';
int get _b => throw '';
set _a(int a) => throw '';
set _b(int a) => throw '';
}
// OK
class E implements A {
noSuchMethod(invocation) => throw '';
}
// The reason why the private fields are needed is because we're allowed to write this:
void wat(A a) {
print(a._a); // subtypes need to have getter _a
a._a = 123; // subtypes need to have setter _a
}
void main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment