Skip to content

Better handling of enum comparisons #401

Description

@danth

What

Comparing enums with <, >, =, != is not ideal, because adding new entries to the enum could break the original assumptions.

Originally suggested at #392 (comment)

Acceptance Criteria (DoD)

  • No comparisons using < or >
  • Minimal comparisons using = or !=

How

In most cases, a switch without a default is much more robust.

For example:

FileExistenceState convertFileExistenceState(fb::FileExistenceState fb_state)
{
  switch (fb_state)
  {
    case fb::FileExistenceState::Deleted:
      return FileExistenceState::Deleted;
    case fb::FileExistenceState::Exists:
      return FileExistenceState::Exists;
  }

  SCORE_LANGUAGE_FUTURECPP_UNREACHABLE_MESSAGE("Invalid fb::FileExistenceState");
}

Not having a default triggers a compiler warning when a new entry is added to the enum, prompting the developer to consider the desired behaviour and add a new case.

Because it is possible (using casts) to create a value which does not correspond to any enum entry, SCORE_LANGUAGE_FUTURECPP_UNREACHABLE is needed to satisfy the compiler that the function will return in all cases. This will crash the program if an invalid value is seen.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    Status
    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions