Skip to content

[Records] Should pointwise implicit downcasts be allowed in records? #2488

@leafpetersen

Description

@leafpetersen

Consider the following example:

  dynamic d = "hello";
  (int, int) r = (d, 4);

There are two possible inferred types for the record literal.

  • We might choose to infer this as (dynamic, int)
    • In this case this code is a static error, since neither type is dynamic, and the RHS type is not a subtype of the LHS type.
  • We might choose to infer this as (int, int)
    • In this case this code is a runtime error, since there must be an implicit cast added to cast d to int before constructing the record.

Which do we intend?

In general, I'm not keen on adding more implicit downcasts. I'd rather the user make the cast explicit here.

We do push casts inwards for list literals however:

  dynamic d = "hello";
  List<int> l = [d];

fails at runtime, not statically (as it must, since we prefer the context type for nominal types, unlike the structural types).

cc @munificent @eernstg @lrhn @jakemac53 @natebosch @stereotype441 @kallentu @chloestefantsova @johnniwinther @scheglov @srawlins

Metadata

Metadata

Assignees

Labels

recordsIssues related to records.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions