dart-lang / dart-lang/language
Class and function scoped typedefs bound to class/function generics
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
Some code I'm working on has some fairly deeply nested objects and the type declarations have gotten a bit hairy due to all the generics floating around. Fortunately, the new non-function typedefs help with that somewhat, allowing me to define things like:
`typedef SubAtomResult = Result, X>, S, T>;`
Unfortunately, this still requires me to give the type of a Result of an Atom of a Subunit as `SubAtomResult`, which is both a bit cumbersome to type, but also difficult to keep track of which of my typedefs need which generics specified as part of them.
In my use case, this typedef is used only in a single class, which itself has generics for ``. Additionally, any places where this typedef are used in this class are within a function which has a generic of `` in it. (Here I've given the generics distinct names for the purpose of disambiguation in the sentence that follows). This means that to use this type, I need to essentially specify the mapping of the `<_2>` generics values to each of the typedef's generics, as in `SubAtomResult subAtomResult = ...`.
My proposal would be to allow typedefs to be defined inside a class or function. This would limit their scope to that class/function, just as is the case for any variable declaration, and they would be able to be bound to the generics of that class/function in the declaration. Below is a more complete bit of code which shows the general syntax of what I would like to be able to do.
```
class Result {}
class Atom {}
class Processor {
List> processAnything(List things) {
List> result = dostuff(things);
return result;
}
List, X>, S, T>> processAtoms(List> atoms) {
List, X>, S, T>> result = dostuff>(atoms);
return result;
}
}
class Processor {
typedef ProcessorResult = Result;
List> processAnything(List things) {
List> result = dostuff(things);
return result;
}
List, X>>> processAtoms(List> atoms) {
typedef SubAtomResult = Result, X, S, T>>;
List result = dostuff>(atoms);
return result;
}
}
```
Related, currently chaining typedefs with generics is awkward and tends to make their use even more cumbersome. For example
```
typedef SubAtom = Atom, X>;
typedef AtomResult, U extends StringUnit, X, S extends Language, T extends Language> = Result;
typedef SubAtomResult, U extends StringUnit, X, S extends Language, T extends Language> = Result;
```
If these were defined inside a class/function which already defined the generics of `` though, then the above could be simplified to something like
```
class Processor {
List, X>, S, T>> processAtoms(List> atoms) {
typedef SubAtom = Atom, X>;
typedef AtomResult = Result, S, T>;
typedef SubAtomResult = Result;
}
}
```
Contributor guide
Research direction
Start by reading the proposal and its examples in this issue, then inspect the Dart language repository for the relevant type-system and typedef design material. Determine the intended scoping, generic binding, chaining, and function-level syntax before implementation; done means the semantics and syntax are specified and the complete proposal is resolved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100