Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instance field should be allowed in initializer expression when it is initialized via "this.foo" sugar. #3263

Closed
DartBot opened this issue May 27, 2012 · 4 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-as-intended Closed as the reported issue is expected behavior type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented May 27, 2012

This issue was originally filed by [email protected]


TO REPRODUCE:
Try to create a new Arc: "var arc = new Arc.startingAt(Point.origin, 25.0, 0.0, 4.0);"

/// Immutable 2D cartesian point.
class Point2d {
  final double x,y;
  const Point2d(this.x, this.y);
  const Point2d.polar(double radians, [double radius = 1.0, Point2d origin = Point2d.origin]) :
    x = radius * Math.cos(radians) + origin.x,
    y = radius * Math.sin(radians) + origin.y;
  
  Point2d operator +(Point2d p) => new Point2d(x + p.x, y + p.y);
  Point2d operator -(Point2d p) => new Point2d(x - p.x, y - p.y);
  
  static final Point2d origin = const Point2d(0.0, 0.0);
}

/// Immutable planar arc.
class Arc {
  final double radius, startRadians, endRadians;
  final Point2d center;
  
  Arc.startingAt(
    Point2d startPosition,
    this.radius,
    this.startRadians,
    this.endRadians) : center = startPosition - new Point2d.polar(startRadians, radius);
}

main() {
  var arc = new Arc.startingAt(Point2d.origin, 25.0, 0.0, 4.0);
}

EXPECTED OUTPUT
Arc is instantiated; main() exits normally.

OBSERVED OUTPUT
In editor:
    Cannot access an instance field in an initializer expression
At runtime:
    Error: line 31 pos 48: illegal access to 'this'

PRODUCT / OS VERSION
Dart Editor build 7905. OS X 10.7.4

ADDITIONAL INFORMATION
I can't think of a reason to disallow access to an instance field when it appears in the parameter list using the "this.foo" sugar (especially since there is no other name that we can use to refer to it).

I can easily work around this by not using the "this.foo" sugar, but the sugar is sweet :-)

@kasperl
Copy link

kasperl commented May 29, 2012

Added Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented May 29, 2012

Access to this is disallowed in the initializer list for a reason: we can ensure consistent initialization.


Set owner to @gbracha.
Removed Type-Defect label.
Added Type-Enhancement, AsDesigned labels.

@DartBot
Copy link
Author

DartBot commented May 30, 2012

This comment was originally written by [email protected]


I understand the reason for disallowing "this" in the initializer list, or maybe I don't :-) It seems like in this particular case, "this.foo" can be considered not an access to a field in "this", but rather as a name for an otherwise unnamed parameter.

For my education, could you briefly elaborate on why this is a bad idea?

@gbracha
Copy link
Contributor

gbracha commented May 30, 2012

Uniform rules are hugely important. Special cases are, as a rule, a bad idea. While they look tempting based on a single example, unforeseen effects compound. So as a language designer, one develops a distinct antipathy for them on principle. Every such "featurette" also has an opportunity cost - time spent on it comes at the direct expense of something else.

This particular case is not worth spending cycles on. The sugar is there for a very simple, common case. If things are more complex, use the full length form. It won't actually be much longer.

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-as-intended Closed as the reported issue is expected behavior labels May 30, 2012
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
copybara-service bot pushed a commit that referenced this issue Dec 5, 2022
Revisions updated by `dart tools/rev_sdk_deps.dart`.

dartdoc (https://github.com/dart-lang/dartdoc/compare/dc502d0..4d7dc93):
  4d7dc93b  2022-12-05  dependabot[bot]  Bump github/codeql-action from 2.1.31 to 2.1.35 (#3263)
  bcf8b6e8  2022-12-05  Parker Lougheed  Weight enums the same as classes for searching (#3260)
  7d95578b  2022-12-04  Parker Lougheed  Update template descriptions (#3258)
  d558f043  2022-12-04  Parker Lougheed  Fix error when using base element href (#3256)
  c3663762  2022-12-04  Parker Lougheed  Add unnecessary override ignore to fix build (#3257)

http (https://github.com/dart-lang/http/compare/976bd56..46a7708):
  46a7708  2022-12-02  Brian Quinlan  Remove binary artifact (#833)

sync_http (https://github.com/dart-lang/sync_http/compare/f5c1f18..8622614):
  8622614  2022-12-02  Kevin Moore  blast_repo fixes (#32)

test (https://github.com/dart-lang/test/compare/f3d80a6..4dceb87):
  4dceb87c  2022-12-01  Nate Bosch  Ignore some usage of dperecated errors (#1807)

webdev (https://github.com/dart-lang/webdev/compare/91b8a19..e39506e):
  e39506e  2022-12-05  Anna Gringauze  Pre-warm expression compiler to speed up Flutter Inspector page loading. (#1786)
  9b19b3b  2022-12-02  Elliott Brooks (she/her)  Can save storage objects in both `session` and `local` storage (#1807)
  e75c45e  2022-12-02  Elliott Brooks (she/her)  Injected client adds `isFlutterApp` to global window object (#1806)
  ba5e3ec  2022-12-01  Elliott Brooks (she/her)  `DebugSession` listens to events instead of just sending events (#1804)

Change-Id: I881d02e966b763879df72b29653a9f241b71eb3d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/273826
Commit-Queue: Devon Carew <[email protected]>
Reviewed-by: Nate Bosch <[email protected]>
copybara-service bot pushed a commit that referenced this issue Jan 19, 2023
This is an accumulation of 12 commits to dartdoc, imported into
dart-lang/sdk across 4 commits, noted below. I've also stripped out
the changes made to any other repository in this DEPS file.

Revisions updated by `dart tools/rev_sdk_deps.dart`.

From cabf333:
dartdoc (https://github.com/dart-lang/dartdoc/compare/eb90a44..1f42216):
  1f422163  2022-12-13  Sam Rawlins  Bump to 6.1.5 (#3274)

From a3b7a76:
  eb90a44c  2022-12-12  Sam Rawlins      Remove annotations from the features section of each element (#3268)
  16126376  2022-12-12  Parker Lougheed  Don't show null in search results when no description (#3273)
  3ff7aa75  2022-12-12  Sam Rawlins      Fix search arrows by using a flat list of suggestions (#3271)
  90264fb4  2022-12-12  dependabot[bot]  Bump github/codeql-action from 2.1.35 to 2.1.36 (#3270)
  bede500d  2022-12-12  Sam Rawlins      Fix remote linking in 2.18 and 3.0.0 (#3267)

From 0c14719:
dartdoc (https://github.com/dart-lang/dartdoc/compare/4d7dc93..f2bb6e9):
  f2bb6e92  2022-12-07  Sam Rawlins  Bump to 6.1.4 (#3266)

From 3a1d7c7:
dartdoc (https://github.com/dart-lang/dartdoc/compare/dc502d0..4d7dc93):
  4d7dc93b  2022-12-05  dependabot[bot]  Bump github/codeql-action from 2.1.31 to 2.1.35 (#3263)
  bcf8b6e8  2022-12-05  Parker Lougheed  Weight enums the same as classes for searching (#3260)
  7d95578b  2022-12-04  Parker Lougheed  Update template descriptions (#3258)
  d558f043  2022-12-04  Parker Lougheed  Fix error when using base element href (#3256)
  c3663762  2022-12-04  Parker Lougheed  Add unnecessary override ignore to fix build (#3257)

Change-Id: I21fc9ecd36db78b7ef420bd179af4907e667557d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279320
Reviewed-by: Kevin Moore <[email protected]>
Commit-Queue: Kevin Chisholm <[email protected]>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-as-intended Closed as the reported issue is expected behavior type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants