{"action":"created","issue":{"url":"https://api.github.com/repos/dart-lang/dev_compiler/issues/52","labels_url":"https://api.github.com/repos/dart-lang/dev_compiler/issues/52/labels{/name}","comments_url":"https://api.github.com/repos/dart-lang/dev_compiler/issues/52/comments","events_url":"https://api.github.com/repos/dart-lang/dev_compiler/issues/52/events","html_url":"https://github.com/dart-lang/dev_compiler/issues/52","id":58148312,"number":52,"title":"shadowing fields with fields","user":{"login":"jmesserly","id":1081711,"avatar_url":"https://avatars.githubusercontent.com/u/1081711?v=3","gravatar_id":"","url":"https://api.github.com/users/jmesserly","html_url":"https://github.com/jmesserly","followers_url":"https://api.github.com/users/jmesserly/followers","following_url":"https://api.github.com/users/jmesserly/following{/other_user}","gists_url":"https://api.github.com/users/jmesserly/gists{/gist_id}","starred_url":"https://api.github.com/users/jmesserly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmesserly/subscriptions","organizations_url":"https://api.github.com/users/jmesserly/orgs","repos_url":"https://api.github.com/users/jmesserly/repos","events_url":"https://api.github.com/users/jmesserly/events{/privacy}","received_events_url":"https://api.github.com/users/jmesserly/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/dart-lang/dev_compiler/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/dart-lang/dev_compiler/labels/js","name":"js","color":"fef2c0"}],"state":"open","locked":false,"assignee":{"login":"jmesserly","id":1081711,"avatar_url":"https://avatars.githubusercontent.com/u/1081711?v=3","gravatar_id":"","url":"https://api.github.com/users/jmesserly","html_url":"https://github.com/jmesserly","followers_url":"https://api.github.com/users/jmesserly/followers","following_url":"https://api.github.com/users/jmesserly/following{/other_user}","gists_url":"https://api.github.com/users/jmesserly/gists{/gist_id}","starred_url":"https://api.github.com/users/jmesserly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmesserly/subscriptions","organizations_url":"https://api.github.com/users/jmesserly/orgs","repos_url":"https://api.github.com/users/jmesserly/repos","events_url":"https://api.github.com/users/jmesserly/events{/privacy}","received_events_url":"https://api.github.com/users/jmesserly/received_events","type":"User","site_admin":false},"milestone":null,"comments":6,"created_at":"2015-02-18T23:52:22Z","updated_at":"2015-04-16T17:16:38Z","closed_at":null,"body":"In Dart when you extend a class with a field with the same name--let's call it `x`--is the old field gets orphaned and the getter for `x` only ever returns the most derived slot. [edit: removed poorly worded sentence, thanks for comments!]\r\n\r\nHere's an example case. mixins for illustration, but the problem doesn't require mixins to manifest:\r\n\r\n```dart\r\ninit(x) {\r\n  print(x);\r\n  return x;\r\n}\r\n\r\nclass Base {\r\n  var x = init('Base.x');\r\n}\r\nclass Mixin1 {\r\n  var x = init('Mixin1.x');\r\n}\r\nclass Mixin2 {\r\n  var x = init('Mixin2.x');\r\n}\r\nclass Derived extends Base with Mixin1, Mixin2 {\r\n  var x = init('Derived.x');\r\n}\r\n\r\nmain() {\r\n  print('Created Derived with x: ' + new Derived().x);\r\n}\r\n```\r\n\r\nprints on Dart VM:\r\nDerived.x\r\nMixin2.x\r\nMixin1.x\r\nBase.x\r\nCreated Derived with x: Derived.x\r\n\r\nso, options. Assuming we can make you pay for this only in derived classes (and doesn't mess with separate compilation), a getter/setter would work. But it could also be an error, depending on how common this actually is.\r\n"},"comment":{"url":"https://api.github.com/repos/dart-lang/dev_compiler/issues/comments/93788458","html_url":"https://github.com/dart-lang/dev_compiler/issues/52#issuecomment-93788458","issue_url":"https://api.github.com/repos/dart-lang/dev_compiler/issues/52","id":93788458,"user":{"login":"jmesserly","id":1081711,"avatar_url":"https://avatars.githubusercontent.com/u/1081711?v=3","gravatar_id":"","url":"https://api.github.com/users/jmesserly","html_url":"https://github.com/jmesserly","followers_url":"https://api.github.com/users/jmesserly/followers","following_url":"https://api.github.com/users/jmesserly/following{/other_user}","gists_url":"https://api.github.com/users/jmesserly/gists{/gist_id}","starred_url":"https://api.github.com/users/jmesserly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmesserly/subscriptions","organizations_url":"https://api.github.com/users/jmesserly/orgs","repos_url":"https://api.github.com/users/jmesserly/repos","events_url":"https://api.github.com/users/jmesserly/events{/privacy}","received_events_url":"https://api.github.com/users/jmesserly/received_events","type":"User","site_admin":false},"created_at":"2015-04-16T17:16:38Z","updated_at":"2015-04-16T17:16:38Z","body":"CC @jacob314 and @leafpetersen as we were chatting about this one yesterday.\r\n\r\nContinuing my example above, the way Dart's \"induced\" fields map to ES6 is conceptually like:\r\n```js\r\nclass Base extends core.Object {\r\n  get foo() { return 123; }\r\n}\r\nlet foo = Symbol('foo');\r\nclass Derived extends Base {\r\n  Derived() { this[foo] = 42; }\r\n  get foo() { return this[foo]; }\r\n  set foo(x) { this[foo] = x; }\r\n}\r\n```\r\n\r\nNow, we could avoid that in many cases (if we had closed world assumption--which we don't), but it's needed when a field is used to implement a getter. That's a decently common pattern. I think the above pattern isn't too odd in ES6... here's an idiomatic private field with public getter:\r\n```js\r\nlet foo = Symbol('foo');\r\nclass Foo {\r\n  constructor() { this[foo] = 42; }\r\n  get foo() { return this[foo]; }\r\n  // ... class mutates this[foo] from some methods ...\r\n}\r\n```\r\n\r\nSo, the fix for this almost ready. One observation from looking at output: library-private fields rarely need this treatment, ditto public fields on library-private classes. A _lot_ of fields seem to be private, so we can eliminate many cases of getter/setter pattern. So it's better than I had feared. Anyway, if we do want to turn it off, we'll have a knob where we could tweak it, but so far I'm inclined to just go for correctness here and see if it becomes a problem.\r\n"}}