Skip to content

Complete matching of array vs partial matching of object #243

@Pyrolistical

Description

@Pyrolistical

Correct me if I am wrong, but the array pattern requires a complete match whereas object pattern only requires a partial match.

array pattern matches completely

match ([1, 2]) {
  when ([x]): // does not match
  when ([x, y, z]): // does not match
  when ([x, y]): // matches
  when ([x, ...rest]): // would match if not for previous case
}

object pattern matches partially

match ({x: 1, y: 2}) {
  when ({x}): // matches
  when ({x, y, z}): // does not match
  when ({x, y}): // would match if not for first case
  when ({x, ...rest}): // would match if not for first case
}

Isn't this going to be confusing? Doesn't this violate the principle of least surprise? Wouldn't it be more consistent and less surprising if object pattern required a complete match?

if object pattern were to require a complete match

match ({x: 1, y: 2}) {
  when ({x}): // does not match
  when ({x, y, z}): // does not match
  when ({x, y}): // matches
  when ({x, ...rest}): // would match if not for previous case
}

It then begs the question on what it means to "completely match an object". I believe the principle of least surprise would be to require objects to match all enumerable properties.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions