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

Shorthand constructor this.x parameters prohibit access to x in initializer list #1970

Closed
sethladd opened this issue Mar 4, 2012 · 2 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug

Comments

@sethladd
Copy link
Contributor

sethladd commented Mar 4, 2012

This may be a known trade-off, but I was curious if this was a bug.

I tried to do:

class Point {
  final num x;
  final num y;
  final num distanceFromOrigin;
  
  Point(this.x, this.y) : distanceFromOrigin = calcDistance(x, y);
  
  static calcDistance(_x, _y) => Math.sqrt((_x*_x) + (_y*_y));
}

But the Editor (rightly so?) said "cannot access field in initializer expression". I understand 'this' isn't available in initializer lists, but I was a little surprised that I couldn't access the implicit x from the constructor parameter.

I ended up writing:

class Point {
  final num x;
  final num y;
  final num distanceFromOrigin;
  
  Point(x, y) :
    x = x,
    y = y,
    distanceFromOrigin = calcDistance(x, y);
  
  static calcDistance(_x, _y) => Math.sqrt((_x*_x) + (_y*_y));
}

Sounds like the tradeoff is "use the shorthand constructor parameter names or initializer lists"

@gbracha
Copy link
Contributor

gbracha commented Mar 5, 2012

This is a known trade off. If you need to use the incoming parameters for more than just setting the corresponding fields, you can do so in the constructor body, but not in the initializer list.


Removed Type-Defect label.
Added Type-Enhancement, WontFix labels.

@gbracha
Copy link
Contributor

gbracha commented Jul 24, 2012

Issue #4181 has been merged into this issue.

@sethladd sethladd added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Jul 24, 2012
@kevmoo kevmoo added closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug and removed resolution-wont_fix labels Mar 1, 2016
copybara-service bot pushed a commit that referenced this issue Mar 14, 2023
Revisions updated by `dart tools/rev_sdk_deps.dart`.

dartdoc (https://github.com/dart-lang/dartdoc/compare/98fa859..7fde7a4):
  7fde7a4d  2023-03-13  dependabot[bot]  Bump github/codeql-action from 2.2.5 to 2.2.6 (#3363)

http (https://github.com/dart-lang/http/compare/805a147..74f9d3d):
  74f9d3d  2023-03-09  Brian Quinlan  Add conformances test that verify that the Client works in Isolates (#889)
  ee03604  2023-03-09  Brian Quinlan  Add a flag to allow the default Client to be tree shaken away. (#868)
  2039fb3  2023-03-09  Brian Quinlan  Fix a reference count race with forwarded delegates. (#888)

matcher (https://github.com/dart-lang/matcher/compare/c1a0704..985e4ef):
  985e4ef  2023-03-10  Sigurd Meldgaard  Improve mismatch description of `contains` (#206)
  5bc765d  2023-03-09  Nate Bosch  Copy expect and async matchers from test package (#210)

shelf (https://github.com/dart-lang/shelf/compare/e3cfe79..9a792b4):
  9a792b4  2023-03-13  Kevin Moore  Update format for latest v3 SDK (#340)
  c8094b7  2023-03-13  Kevin Moore  Update no-response.yml - once a day (#339)

test (https://github.com/dart-lang/test/compare/3ba78f1..0e5c028):
  0e5c028d  2023-03-10  Nate Bosch  Only use environment variable for chrome (#1970)
  0b08d704  2023-03-08  Nate Bosch  Add a hooks_testing library (#1952)
  aacee2c7  2023-03-08  Nate Bosch  Retry filesystem deletes (#1965)

webdev (https://github.com/dart-lang/webdev/compare/cfe9753..49013b8):
  49013b8  2023-03-09  Elliott Brooks (she/her)  [MV3 Debug Extension] Fix authentication issue for the Dart Debug Extension (#2026)
  c8d4439  2023-03-09  Elliott Brooks (she/her)  Support `Set` inspection in DWDS (#2024)
  442639d  2023-03-08  Elliott Brooks (she/her)  Handle unexpected extension debugger disconnection events without crashing app (#2021)

yaml_edit (https://github.com/dart-lang/yaml_edit/compare/6abc42a..fbc5cb3):
  fbc5cb3  2023-03-09  Kevin Moore  Require Dart 2.19, update to latest lints
  c9e82f0  2023-03-09  Mohamed Ishad  Fix YamlEditor.update method leaving trailing spaces at eol (#42)

Change-Id: Idc785347448e6a56e26d9d46b3666440d59c22de
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/288801
Auto-Submit: Devon Carew <devoncarew@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
copybara-service bot pushed a commit that referenced this issue Mar 14, 2023
…edit"

This reverts commit fcb8f80.

Reason for revert: google3 roll failure

Original change's description:
> [deps] rev dartdoc, http, matcher, shelf, test, webdev, yaml_edit
>
> Revisions updated by `dart tools/rev_sdk_deps.dart`.
>
> dartdoc (https://github.com/dart-lang/dartdoc/compare/98fa859..7fde7a4):
>   7fde7a4d  2023-03-13  dependabot[bot]  Bump github/codeql-action from 2.2.5 to 2.2.6 (#3363)
>
> http (https://github.com/dart-lang/http/compare/805a147..74f9d3d):
>   74f9d3d  2023-03-09  Brian Quinlan  Add conformances test that verify that the Client works in Isolates (#889)
>   ee03604  2023-03-09  Brian Quinlan  Add a flag to allow the default Client to be tree shaken away. (#868)
>   2039fb3  2023-03-09  Brian Quinlan  Fix a reference count race with forwarded delegates. (#888)
>
> matcher (https://github.com/dart-lang/matcher/compare/c1a0704..985e4ef):
>   985e4ef  2023-03-10  Sigurd Meldgaard  Improve mismatch description of `contains` (#206)
>   5bc765d  2023-03-09  Nate Bosch  Copy expect and async matchers from test package (#210)
>
> shelf (https://github.com/dart-lang/shelf/compare/e3cfe79..9a792b4):
>   9a792b4  2023-03-13  Kevin Moore  Update format for latest v3 SDK (#340)
>   c8094b7  2023-03-13  Kevin Moore  Update no-response.yml - once a day (#339)
>
> test (https://github.com/dart-lang/test/compare/3ba78f1..0e5c028):
>   0e5c028d  2023-03-10  Nate Bosch  Only use environment variable for chrome (#1970)
>   0b08d704  2023-03-08  Nate Bosch  Add a hooks_testing library (#1952)
>   aacee2c7  2023-03-08  Nate Bosch  Retry filesystem deletes (#1965)
>
> webdev (https://github.com/dart-lang/webdev/compare/cfe9753..49013b8):
>   49013b8  2023-03-09  Elliott Brooks (she/her)  [MV3 Debug Extension] Fix authentication issue for the Dart Debug Extension (#2026)
>   c8d4439  2023-03-09  Elliott Brooks (she/her)  Support `Set` inspection in DWDS (#2024)
>   442639d  2023-03-08  Elliott Brooks (she/her)  Handle unexpected extension debugger disconnection events without crashing app (#2021)
>
> yaml_edit (https://github.com/dart-lang/yaml_edit/compare/6abc42a..fbc5cb3):
>   fbc5cb3  2023-03-09  Kevin Moore  Require Dart 2.19, update to latest lints
>   c9e82f0  2023-03-09  Mohamed Ishad  Fix YamlEditor.update method leaving trailing spaces at eol (#42)
>
> Change-Id: Idc785347448e6a56e26d9d46b3666440d59c22de
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/288801
> Auto-Submit: Devon Carew <devoncarew@google.com>
> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>

Change-Id: If7dd2027f4b6ead777eaf1b3cf7561d4dced4095
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/288802
Auto-Submit: Devon Carew <devoncarew@google.com>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Commit-Queue: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
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-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants