Skip to content

Lexical scope and inheritance scope - clarification and motivation. #1180

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

Closed
rakudrama opened this issue Jan 14, 2012 · 4 comments
Closed

Lexical scope and inheritance scope - clarification and motivation. #1180

rakudrama opened this issue Jan 14, 2012 · 4 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). type-enhancement A request for a change that isn't a bug
Milestone

Comments

@rakudrama
Copy link
Member

Peter requested that the language issue be discussed in a different bug to Issue #641.

According to the current lookup rules, lexical scope is exhausted before considering inheritance scope.

The following program should print 89:

  int x = 89;

  class Foo {
    int x = 42;
  }

  class Bar extends Foo {
    m() => x;
  }

  main() {
    print(new Bar().m());
  }

Do we have the same answer if we replace 'x' with 'x()' and '=' with '=>' ?

I am not alone in finding that this rule feels strange, so I think the motivation needs to be explained to the Dart community.

On the surface, it seems that collections of classes will become fragile as new global definitions can usurp the lookup path. Design patterns that use implementation inheritance will be particularly susceptible. Our target market, web applications, has a large user-interface component, and user interface libraries often use implementation inheritance to customize the UI - subclassing interaction units like menus, buttons etc. To avoid accidents, programmers will have to make use of defensive 'this.' and 'super.', which will clutter the application code.

The cohesion between the classes in a hierarchy is usually much higher that between a leaf class and the global environment. Programming languages should make the common case convenient ahead of the less common.
Dart appears to put the less common ahead of the common.

Why is Dart's lookup order like this?
What are the advantages?

@rakudrama
Copy link
Member Author

Marked this as blocking #641.

@gbracha
Copy link
Contributor

gbracha commented Jan 21, 2012

A future rev of the spec will give an extended rationale. At its root, the issue is capture of lexical scope by superclasses. Lexical scope is under the control of the developer of a library, whereas superclasses are not. Hence, it is not the library scope (Dart has no global scope) that "usurps" the lookup path, but the opposite.

Put another way - class hierarchies within a library have high cohesion, but when relying crossing library boundaries they do not. Assuming they do is a common mistake, repeated in several earlier programming languages.

Warnings about shadowing should probably be refined to warn about these situations - though proper tooling would be enough (e.g., highlight any inherited name that is hidden by the lexical scope).

There are also considerations with respect to future evolution of the language.

Concrete scenarios illustrating these issues will come in due course.


Set owner to @gbracha.
Added Accepted label.

@gbracha
Copy link
Contributor

gbracha commented May 24, 2012

Added this to the M1 milestone.

@gbracha
Copy link
Contributor

gbracha commented May 29, 2012

Added some discussion and a basic example to section 3.1.


Added Done label.

@rakudrama rakudrama added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels May 29, 2012
@rakudrama rakudrama added this to the M1 milestone May 29, 2012
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
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). type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants