Skip to content

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

Closed
@rakudrama

Description

@rakudrama

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?

Metadata

Metadata

Assignees

Labels

area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-enhancementA request for a change that isn't a bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions