dart-lang / dart-lang/language

Typedefs in scopes other than the top level

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

Description

Currently `typedef` declaration can only occur as top-level declarations in a library.

In some cases, you would want to just create a local shorthand for some code. To do that, you now have to put the declaration at the top level, probably with a private name to avoid it leaking, and then use it far from the declaration. That gives bad code locality and convenience of writing.

I propose that typedefs should be allowed "anywhere", or At least at top level, as a static class declaration and as a local declaration in a method body. Effectively anywhere a constant can be declared.
The scoping should be the same as for other declarations. In particular, a local declaration has access to class and method type variables. Since typedefs are structural aliases, that should not be a problem, the declaration can be expanded at the point of use without issue.

With dart-lang/sdk#66, this becomes even more useful.

Example:
```dart
class MyFuture implements Future {
Future catchError(Function handler, [bool test(Object error)]) {
typedef Unary = Function(Null);
if (handler is Unary) handler = (o, StackTrace stack) => handler(i);
... handler ..
}
}
```

I'm sure there are lots of details to get right, like whether static typedefs need the `static` in front, but something like this would be nice.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.