From b81e491ec5f3bdeb59d0932e44eb2f45edfe2e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=AA=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=82=E0=A4=B8=20?= =?UTF-8?q?=E0=A4=95=E0=A5=81=E0=A4=AE=E0=A4=BE=E0=A4=B0=20=E0=A4=A7?= =?UTF-8?q?=E0=A4=A8=E0=A4=96=E0=A4=A1=E0=A4=BC?= <71747203+pdhankhar61@users.noreply.github.com> Date: Thu, 4 Jan 2024 00:58:19 +0530 Subject: [PATCH] Clarify use of `this` in initializing instance variables (#5310) Co-authored-by: Marya <111139605+MaryaBelanger@users.noreply.github.com> Co-authored-by: Marya Belanger Co-authored-by: Erik Ernst Co-authored-by: Eric Windmill Co-authored-by: Eric Windmill Co-authored-by: Parker Lougheed --- .../lib/language_tour/classes/point_this.dart | 16 +++++++++ src/language/classes.md | 36 +++++++++++++++---- 2 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 examples/misc/lib/language_tour/classes/point_this.dart diff --git a/examples/misc/lib/language_tour/classes/point_this.dart b/examples/misc/lib/language_tour/classes/point_this.dart new file mode 100644 index 0000000000..a2135e9737 --- /dev/null +++ b/examples/misc/lib/language_tour/classes/point_this.dart @@ -0,0 +1,16 @@ +// ignore_for_file: invalid_reference_to_this, unnecessary_this +double initialX = 1.5; + +class Point { + // OK, can access declarations that do not depend on `this`: + double? x = initialX; + + // ERROR, can't access `this` in non-`late` initializer: + double? y = this.x; + + // OK, can access `this` in `late` initializer: + late double? z = this.x; + + // OK, `this.fieldName` is a parameter declaration, not an expression: + Point(this.x, this.y); +} diff --git a/src/language/classes.md b/src/language/classes.md index db31a20ca9..3f7cca1e0d 100644 --- a/src/language/classes.md +++ b/src/language/classes.md @@ -167,7 +167,9 @@ class Point { } ``` -All uninitialized instance variables have the value `null`. +An uninitialized instance variable declared with a +[nullable type][] has the value `null`. +Non-nullable instance variables [must be initialized][] at declaration. All instance variables generate an implicit *getter* method. Non-final instance variables and @@ -175,11 +177,6 @@ Non-final instance variables and an implicit *setter* method. For details, check out [Getters and setters][]. -If you initialize a non-`late` instance variable where it's declared, -the value is set when the instance is created, -which is before the constructor and its initializer list execute. -As a result, non-`late` instance variable initializers can't access `this`. - ```dart class Point { @@ -195,6 +192,31 @@ void main() { } ``` +Initializing a non-`late` instance variable where it's declared +sets the value when the instance is created, +before the constructor and its initializer list execute. +As a result, the initializing expression (after the `=`) +of a non-`late` instance variable can't access `this`. + + +```dart +double initialX = 1.5; + +class Point { + // OK, can access declarations that do not depend on `this`: + double? x = initialX; + + // ERROR, can't access `this` in non-`late` initializer: + double? y = this.x; + + // OK, can access `this` in `late` initializer: + late double? z = this.x; + + // OK, `this.fieldName` is a parameter declaration, not an expression: + Point(this.x, this.y); +} +``` + Instance variables can be `final`, in which case they must be set exactly once. Initialize `final`, non-`late` instance variables @@ -350,3 +372,5 @@ can pass a static method as a parameter to a constant constructor. [initializer list]: /language/constructors#initializer-list [factory constructor]: /language/constructors#factory-constructors [late-final-ivar]: /effective-dart/design#avoid-public-late-final-fields-without-initializers +[nullable type]: /null-safety/understanding-null-safety#using-nullable-types +[must be initialized]: /null-safety/understanding-null-safety#uninitialized-variables \ No newline at end of file