dart-lang / dart-lang/language

Parametric type parameters?

Open
#3,620 9 comments 5 reactions 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

We could support a modifier on formal type parameters indicating that it is never needed at run time, that is, no operations are performed where the value of the actual type argument passed to that formal type parameter is needed in order to obtain the correct semantics. For example:

```dart
X id(X x) => x; // `X` is parametric.
bool foo(Object o) => o is X; // `X` is not parametric.
```

A parametric type parameter does not have to have a representation at run time. This implies that it could be useful from a performance perspective to mark a given type parameter as parametric, if this is possible.

It isn't always possible, of course: It is a compile-time error to use a parametric type parameter in a way that has a run-time semantics. So if `X` is parametric then we can't do `e is X` for any `e`, or `e as List`, we can't evaluate a list literal like `[]`, can't do `print(X)`, etc. It is also a compile-time error to use a parametric type parameter as an actual type argument (or as a subterm of an actual type argument) which is passed to a type parameter that isn't parametric.

```dart
// Strawman syntax, let's just use a verbose keyword.

X id(X x) => x; // OK.
bool foo(Object o) => o is X; // Compile-time error at `is X`.
void bar() => id; // OK.
```

It is more complicated, but it should also be possible to mark a type parameter of a class, mixin, enum, or mixin class as parametric, and maintain the same strict checking that it is indeed not needed at run time.

For dynamic invocations, we would need to eliminate any actual arguments that are passed to parametric type parameters as part of the dynamic invocation semantics itself. I don't know if this is going to be difficult, but it seems reasonable to assume that it is at least possible.

Note that a traditional motivation for having parametric type parameters is that they are associated with the ability to prove certain statements about a program. See [Theorems for free](https://people.mpi-sws.org/~dreyer/tor/papers/wadler.pdf) for more about this topic. Dart will probably not have any strict and formal provability properties like this, but it is certainly true that the actual type argument cannot influence the behavior of a function invocation if that type argument does not have a run-time representation at all.

If we introduce parametric type parameters then we could also use this to enhance other kinds of checking: We might want to lint every location where a non-transparent extension type is used as an actual type argument for a formal type parameter that isn't parametric.

Another indirect benefit could be that it might be possible to eliminate a larger set of type parameters during compilation if we introduce parametric type parameters: We can safely eliminate all the parametric type parameters during compilation, but this could make it provable that some other formal type parameters can also be eliminated, possibly using a fixed point iteration to get rid of as many type parameters as possible.

@dart-lang/language-team, WDYT?

@mraleph, @mkustermann, @sigmundch, @rakudrama, how important do you think it would be to enhance the support for elimination of type parameters at run time? Does it actually reduce the time/space consumption by large programs in a way that matters?

Contributor guide

Open the contributing guide

Research direction

The issue names no implementation files, tests, or entry points. Start by reading the proposal and its examples, then determine the specification and implementation work needed to define parametric type parameters and their run-time restrictions. Done would require an accepted design with a corresponding implementation and testing plan.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.