Solution: Enumerated Types

Explore the solution for the Enumerated types challenge.

Solution: Enumerated Types

The app can have one of the following states:

  • done: The app received the response successfully.
  • waiting: The app is waiting for the response.
  • error: The app received an error from the server.

Solution #1

Solution #1 is to declare the enumeration Status to represent the above three cases.

enum Status {
  done,
  waiting,
  error,
}

Solution #2

Solution #2 is to print a message for each value inside a switch block. Whenever input status matches one of the Enum values, the corresponding message is printed using the print() method.

  • Status.done: The data is ready!
  • Status.waiting: The app is waiting on a response from the server.
  • Status.error: An error occurred.
  switch(status) {
    case Status.done:
      break;
    case Status.waiting:
      break;
    case Status.error:
      break;     
  }

Solution #3

For the third solution, the delayed value is added to Status.

enum Status {
  ...

  delayed,
}

Adding default block: The default block is added to handle the input status value. When status does not match any case-blocks, it is handled in the default block and prints the default message.

  switch(status) {

    default:
  }   

Check out the complete implementation in the code snippet below. Change the input status to observe the print statement.

Get hands-on with 1400+ tech skills courses.