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

can not match empty object #51

Closed
concavelenz opened this issue Sep 20, 2017 · 3 comments
Closed

can not match empty object #51

concavelenz opened this issue Sep 20, 2017 · 3 comments

Comments

@concavelenz
Copy link

With regard to https://github.com/tc39/proposal-pattern-matching#objects-closed-by-default

Allowing extra properties by default does not provide a means of matching objects without additional properties, {x,y} without property "z", the empty object {}. Requiring explicit ... is strictly more powerful.

It seems odd that it is possible to match empty array but not empty object.

@notriddle
Copy link

It would still ignore hidden properties, like constructor and __proto__, and properties that come from the prototype, right? So the {} pattern would match an {} expression, Object.create(null), and an empty array [] (since length is hidden).

@tabatkins
Copy link
Collaborator

Length of an array is pretty core to the concept of what the array is; the precise set of properties owned by an object does not have the same semantics. As @notriddle notes, most objects will have several non-enumerable properties inherited from Object or just intrinsic; additionally, subclasses will generally have more properties than the superclass, but it's natural to match a subclass instance when you ask if it's a superclass (both respond true to instanceof Superclass). Finally, I think it'll be most common that only some of the properties of the object will be relevant in a given match.

So, if we fail for object with more properties than requested, we'll have to add some corner-case handling by default anyway, and authors will commonly get spurious match failures if they forget to include the .... I think the cases where you want to allow additional properties are more common than wanting a precise object shape (again, ignoring the always-present properties).

@zkat
Copy link
Collaborator

zkat commented Mar 25, 2018

Hey y'all! #65 has gotten merged, and a lot of issues have become irrelevant or significantly changed in context. Because of the magnitude of changes and subtle differences in things that seem similar, we've decided to just nuke all existing issues so we can start fresh. Thank you so much for the contributions and discussions and feel free to create new issues if something seems to still be relevant, and link to the original, related issue so we can have a paper trail (but have the benefit of that clean slate anyway).

As far as this specific one goes: I think the new semantics for object matchers are The Right Thing™ (because partial-matching of objects is such a common thing to want and requiring ...x is a footgun for this). You can achieve whatever kind of "empty object" matching you want (since this varies) using guards:

match (x) {
  x if (_.isEmpty(x)) => ...
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants