Skip to content

Optional Entries in Map Patterns #2496

Open
@ds84182

Description

@ds84182

Currently the specification for patterns states that a map that does not contain a key will not match. Unfortunately this means that patterns aren't able to be used to destructure JSON with optional fields.

Say we have the following schema: {"type": "location", "latlon": [number, number], "name": string?, "visits": int (defaults to 0 if unspecified)}

Matching this is not possible due to the optional fields:

json = {"type": "location", "latlon": [123.4, -56.78]};
return switch (json) {
  case {"type": "location", "latlon": [lat as num, lon as num], "name": name as String?, "visits": visits as int?} => ...
};

Perhaps a ? suffix could be used on the map key to indicate that it should be optional?

Metadata

Metadata

Assignees

No one assigned

    Labels

    patternsIssues related to pattern matching.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions