Skip to content

Instantly share code, notes, and snippets.

@refi64
Created May 19, 2020 18:31
Show Gist options
  • Save refi64/486a6dc9e28bcf3fa9aad369c3d8d137 to your computer and use it in GitHub Desktop.
Save refi64/486a6dc9e28bcf3fa9aad369c3d8d137 to your computer and use it in GitHub Desktop.
Dart "protected methods" via interfaces & mixins
abstract class MyType {
void doStuff();
}
mixin MyTypeHelpers {
void protectedUtilityMethod() => print('Protected method');
}
abstract class MyDerivedType implements MyType {
factory MyDerivedType.create() => MyDerivedTypeImpl();
void doMoreStuff();
}
class MyDerivedTypeImpl with MyTypeHelpers implements MyDerivedType {
void doStuff() {
print('Doing stuff');
protectedUtilityMethod();
}
void doMoreStuff() {
print('Doing more stuff');
protectedUtilityMethod();
}
}
void main() {
var value = MyDerivedType.create();
value.doStuff();
value.doMoreStuff();
// Calling value.protectedUtilityMethod is a compile-time error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment