Skip to content

Can we promote through record fields? #2503

@eernstg

Description

@eernstg

Consider the following example:

void main() {
  (int?, bool) pair = (42, true);
  pair.$0.isEven; // OK?
}

The rule would be that we can specify an arbitrary record structure (here \X, Y. (X?, Y)) and perform pointwise operations on it (transforming \X, Y. (X?, Y) to \X, Y. (X, Y)), and declaring that the latter is a 'type of interest' because it is a structurally lifted version of the standard rule that makes T a type of interest during initialization of a local variable with declared type T?. This allows us to promote from (int?, bool) to (int, bool), from (int?, (bool?, List<String>?)) to (int, (bool, List<String>?)), etc.

A similar phenomenon arises with intersection types, where we are immediately promoting a type which is a type variable X to an intersection type X & T, because it was initialized by an expression of type X & T.

void f<X>(X x) {
  if (x is int) {
    var y = x; // Inferred type of `y` is `X`, and `y` is immediately promoted to `X & int`.
    x = y; // OK.
    int i = y; // OK.
  }
}

Are we going to apply similar promotions based on "lifted" types of interest?

void f<X>(X x) {
  if (x is int) {
    var r = (x, true);
    X y = r.$0; // OK.
    int i = r.$0; // OK?
  }
}

We could proceed as usual (in some lifted sense) and erase the type of the record literal from (X & int, bool) to (X, bool). However, it is actually sound to maintain that the record has the type (X & int, bool), because the run-time type of the record is computed from the run-time types of the field values, and the actual object denoted by x at this point is known to have a type S such that S <: int and S <: X.

But it's a potentially deep violation of the current rules that we even consider a type like (X & int, bool), because an intersection type does not otherwise occur as a subterm of any other type.

In any case, lifting initializer based promotions to record types seems to be a non-trivial exercise.

@natebosch, @lrhn, @munificent, @leafpetersen, @jakemac53, @kallentu, @stereotype441, WDYT?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requestedrecordsIssues related to records.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions