Created
June 11, 2020 16:26
-
-
Save filiph/939c601164ca1daabd89d3dffe5ba5e7 to your computer and use it in GitHub Desktop.
A convoluted viscosity late initialization example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Viscosity {} | |
class Material { | |
Future<Viscosity> computeViscosity() async { | |
return Viscosity(); | |
} | |
} | |
class Goo { | |
late Viscosity v; | |
late Future<bool> isReady; | |
Goo(Material m) { | |
isReady = init(m); | |
} | |
Future<bool> init(Material m) async { | |
v = await m.computeViscosity(); | |
return true; | |
} | |
} | |
Future<void> main() async { | |
var m = Material(); | |
var goo = Goo(m); | |
// If you remove this line, the code breaks at runtime | |
// with "Field 'v' has not been initialized". | |
await goo.isReady; | |
print(goo.v); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment