Created
July 24, 2020 12:54
-
-
Save creativecreatorormaybenot/a5b1ae1e3cbc2a389b713f49fd910a0f to your computer and use it in GitHub Desktop.
late in constructors
This file contains hidden or 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
void main() { | |
// Construct the object first. | |
final baz = Baz(calculate('assignmentInConstructor')); | |
// Call access to access the late variables and initialize them if they are lazy. | |
baz.access(); | |
} | |
class Baz { | |
Baz( | |
// Implicit assignment by the caller. | |
this.assignmentInConstructor, | |
) : assignmentInInitializerList = calculate('assignmentInInitializerList') { | |
assignmentInConstructorBody = calculate('assignmentInConstructorBody'); | |
} | |
late final int | |
assignmentInConstructor; // I expect this to be lazily initialized. | |
late final int | |
assignmentInInitializerList; // I expect this to also be lazily initialized. | |
late final int | |
assignmentInConstructorBody; // I expect this to not be lazily initialized. | |
void access() { | |
print('access'); | |
print('$assignmentInConstructor' | |
'$assignmentInInitializerList' | |
'$assignmentInConstructorBody'); | |
} | |
} | |
int calculate(String message) { | |
print('calcuate $message'); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment