Skip to content

Poor ergonomics when a package developer uses a private enumerated value to prevent exhaustive matching. #61873

@stereotype441

Description

@stereotype441

This came up in a discussion with @brianquinlan.

Sometimes a package author wants to prevent clients from exhaustively switching on an enumerated type, so that in the future they can add enumerated values without breaking clients. One way to do this is to create a dummy private enumerated value (e.g. _unknown); this ensures that it's impossible for a client to exhaustively switch on the enum type; they have to use a default clause or a wildcard pattern.

Unfortunately, this approach isn't very ergonomic, because if the client tries to exhaustively match such an enum type:

  • The problem message they get is "The type TYPE isn't exhaustively matched by the switch cases since it doesn't match the pattern 'TYPE._unknown'". This seems to imply that the user ought to be able to fix the problem by adding a switch case for TYPE._unknown, but they can't.
  • The correction message they get is "Try adding a wildcard pattern or cases that match 'TYPE._unknown'." Again, adding a case that matches TYPE._unknown isn't possible.
  • The analysis server offers a quick fix "Add missing switch cases", which does nothing. Instead, a quick fix should be offered that adds a default clause or a wildcard pattern.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.model-exhaustivenessImplementation of exhaustiveness checking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions