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

Comments

Projects
None yet
4 participants
@DartBot
Copy link

commented May 27, 2012

This issue was originally filed by schwa...@gmail.com


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

This comment has been minimized.

Copy link
Contributor

commented May 29, 2012

Added Area-Language, Triaged labels.

@gbracha

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Author

commented May 30, 2012

This comment was originally written by schwaj...@gmail.com


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

This comment has been minimized.

Copy link
Contributor

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.

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.