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

Wildcard for object pattern #3008

Closed
scheglov opened this issue Apr 18, 2023 · 3 comments
Closed

Wildcard for object pattern #3008

scheglov opened this issue Apr 18, 2023 · 3 comments
Labels
feature Proposed language feature that solves one or more problems patterns Issues related to pattern matching.

Comments

@scheglov
Copy link
Contributor

If we have code like

var o = e;
use(o.foo);
use2(o.foo);
use(o.bar);
use2(o.bar);

Because we see that only foo and bar properties are used, we decide to declare them by destructuring.

var MyVeryLongTypeNameOfTheExpressionAtTheRightHandSize(: foo, : bar) = e;
use(foo);
use2(foo);
use(bar);
use2(bar);

Oh no! The expression e has a very long type name, which we have to repeat in the object pattern.
This kills the whole idea of destructuring.

It would be nice if we did not have to repeat type names, just as we don't write them in var foo = o.foo.
Maybe var _(: foo, : bar) = e;.

@scheglov scheglov added the feature Proposed language feature that solves one or more problems label Apr 18, 2023
@mmcdon20
Copy link

Similar to: #2563

@Mike278
Copy link

Mike278 commented Apr 18, 2023

Yeah I came across this too when experimenting with refactoring some stuff to use patterns etc, I've just been doing:

var (foo, bar) = (e.foo, e.bar);
use(foo);
use2(foo);
use(bar);
use2(bar);

which works well enough for cases like this where you're already writing a statement, but not if you're in an expression context (for example)

@munificent
Copy link
Member

Yes, this issue is exactly the intent behind #2563.

I agree, it's very annoying. An earlier version of the proposal supported exactly this behavior but at the expense of not actually support real record patterns. The record pattern syntax was always just "destructure the thing, whatever it is", which worked well for objects but meant there was no actual pattern syntax for "test if the thing actually is a record and then destructure it".

When I fixed the proposal to make record patterns actually match records (and only records) it broke that affordance.

I believe we can get it back by saying that if the matched value type is not a record type of a supertype of Record then treat a record pattern as an object pattern with an inferred type. Unfortunately, we didn't have time to get that into 3.0, but maybe in a follow-on release.

@munificent munificent closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Proposed language feature that solves one or more problems patterns Issues related to pattern matching.
Projects
None yet
Development

No branches or pull requests

5 participants