Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inferring invariant type arguments in object patterns #2784

Closed
munificent opened this issue Jan 19, 2023 · 1 comment
Closed

Inferring invariant type arguments in object patterns #2784

munificent opened this issue Jan 19, 2023 · 1 comment
Labels
patterns Issues related to pattern matching.

Comments

@munificent
Copy link
Member

Given:

typedef Inv<T> = T Function(T);

Consider:

Object obj = ...
if (obj case Inv()) { ... }

What does this mean? In particular, what type argument do we infer for Inv in the object pattern? Do we infer a type? Or does that pattern just mean "check that the value has the class Inv but then doesn't actually do a full type test?

If we think of it as a class test, it's almost like we treat it as:

Object obj = ...
if (obj case Inv<var T>()) { ... }

Almost as if the case itself is generic. But we don't actually have type patterns.

If we allow this, then what happens if there is further code that expects us to now know a type, like:

Object obj = ...
if (obj case Inv() && var x) { ... }

Is there a type we can promote x to?

Or:

Object obj = ...
if (obj case Inv(call: var y)) { ... }

What is the type of y?

@leafpetersen and I talked about this some and we're currently leaning towards making this an error: If a type in an object pattern has an invariant type parameter and the matched value type doesn't give us enough context to infer a type, then it's simply an error. You either need to use a more specific matched value type (i.e. not Object in the example here), or you need to write the type argument on the pattern (i.e. case Inv<String>() or whatever). There's no way to to write a pattern that means "match Inv for any T".

@munificent munificent added request Requests to resolve a particular developer problem patterns Issues related to pattern matching. and removed request Requests to resolve a particular developer problem labels Jan 19, 2023
@munificent
Copy link
Member Author

Closing as duplicate of #2783.

@munificent munificent closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patterns Issues related to pattern matching.
Projects
None yet
Development

No branches or pull requests

1 participant