Skip to content

Strawman union proposal question regarding intersection #2731

Open
@bernaferrari

Description

@bernaferrari

75% of my union usage in TS is

void method(data: A | B)

As the proposal says, intersection is hard, so maybe for now Dart could avoid data.something where something exists in both A and B.

But... Just having A | B in the parameter and being able to switch between them would be extremely helpful for me!! I hate using dynamic or object. And I hate using a second variable that's not null when the first one is.

So, this would be extremely helpful to me:

int method(data: A | B) =>
switch(data) {
A: return 1, 
B: return 2,
}

Yet I hear "is" is really really expensive. So maybe this is faster than comparing by "is" twice.

Any thoughts?

I'm not sure opening an issue is the correct path, just wanted to contribute to the discussion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions