Skip to content

Instantly share code, notes, and snippets.

@matanlurey
Created September 28, 2022 02:46
Show Gist options
  • Save matanlurey/9ab8e0020aab9592624f9c9db4548674 to your computer and use it in GitHub Desktop.
Save matanlurey/9ab8e0020aab9592624f9c9db4548674 to your computer and use it in GitHub Desktop.
An example of abstract fields in Dart.
// An interface that defines two mutable fields.
abstract class Ball {
abstract int x;
abstract int y;
}
// Implements the fields by initializing in constructor.
class BallImplConstructor implements Ball {
@override
int x;
@override
int y;
BallImplConstructor(this.x, this.y);
}
// Implements the field by intitializing with values.
class BallImplDefault implements Ball {
@override
var x = 0;
@override
var y = 0;
}
// Implements the field by promising it will be initialized.
//
// This is pretty clearly the worst option unless there are some lifecycle
// requirements we've not aware of, like initializing these values in a
// another function.
class BallImplLate implements Ball {
@override
late int x;
@override
late int y;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment