dart-lang / dart-lang/language

Improve the support for managing name spaces locally

Open
#266 0 comments 0 reactions 0 assignees View on GitHub
request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Name space pollution can be a problem: If a library is very large and/or many non-prefixed imports bring a large number of names into scope, it's easy to get into a situation where a name is misinterpreted by a reader of the code, or where a seemingly benign change (adding one more `import`) creates a name clash, which may require a non-trivial amount of refactoring before things are working again.

Dart allows developers to manage name spaces in certain ways, in particular by using prefixed imports, and by using `show` and `hide` in imports.

However, the optimal name space may not be identical for every location in a program, and it is not possible to manage name spaces locally.

For instance, the language team has considered support for accessing enumerated values without having to use the name of the enumeration (so it would be `monday` rather than `Days.monday`).

An example of name space management which is at the core of object-orientation in general is the ability to access members of `this` in instance methods without including the word `this`.

This kind of name space management has been provided elsewhere as well, for instance in Kotlin function literals and types "with receiver" ([here](https://kotlinlang.org/docs/reference/lambdas.html#function-literals-with-receiver) and [here](https://kotlinlang.org/docs/reference/lambdas.html#function-types)):

```kotlin
val repeatFun: String.(Int) -> String = { times -> this.repeat(times) }
```

This function can then be called using a method invocation syntax (`"hello".repeatFun(3)`), and it can be called as a regular function (`repeatFun("hello", 3)`). In the body of the function we can use `this` with type `String`, and at run time `this` is bound to the receiver (that is, the 1st argument for the regular function invocation).

Another example of a mechanism that adds a set of names to the current scope (such that `id1.id2` can be abbreviated to `id2`) is the `open` construct in SML ([see, e.g., p47 of this PDF](http://www.cs.cornell.edu/riccardo/prog-smlnj/notes-011001.pdf)).

All in all, the ability to give access to a certain name space in a specific scope is rather inhomogeneous and inconsistent in Dart, and there are a number of well-tested mechanism that we could use as a starting point for improvements in this area.

Contributor guide

Open the contributing guide

Research direction

No source file, test, or entry point is named. Start by reviewing the issue's examples of local namespace management and the linked Kotlin and SML references; done would require a concrete, consistent improvement proposal for Dart's scope rules.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, kotlin
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.