Simplifying the Day class in enhanced enum chapter

Hi, I find that the Day class code:

enum Day {
  monday,
  tuesday,
  wednesday,
  thursday,
  friday,
  saturday,
  sunday;

  Day operator +(int days) {
  
    final numberOfItems = Day.values.length;
    
    final index = (this.index + days) % numberOfItems;
    
    return Day.values[index];
  }
}

Instead of using Day.values, I find that I can use values directly. I think this make the code more concise and easy to maintain. Because if you later change the name of Day enum, you don’t need to change xxx.values inside.

1 Like

I agree. We should definitely make this simplification. Thank you for finding that.

We’re probably going to do an update midyear when Dart 3 comes out so I’ll save this edit for then.

2 Likes