Skip to content

language: specify if type literals are valid const map keys #21553

Closed
@rakudrama

Description

@rakudrama

The spec as I read is does not tell us whether or not the following program is legal. It depends on whether the implementation of Type overrides operator==(). The VM and dart2js differ in this regard which is a problem for portability as programs developed on the VM don't work on dart2js (#17207)

It would be helpful to have a ruling on whether this specific combination of features is supposed to work and then have it implemented uniformly.

class A {}

const table = const {
    A: 'Apple',
};

main() {
    print(table[A]);
}

Wrapping the type to avoid operator== as follows leads to surprising behaviour.

class A {}
class B {}

class Key {
    final Type type;
    const Key(this.type);
}

const table = const {
    const Key(A): 'Apple',
    const Key(B): 'Banana',
};

main() {
    print(table[const Key(A)]);  // Apple
    print(table[new Key(B)]);    // null
}

Related issues: #17123 #17207

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions