David Koblas (2019-02-28T14:06:05.000Z)
david at koblas.com (2019-02-28T14:07:49.403Z)
Naveen, Thanks for your observation. The example that I gave might have been too simplistic, here's a more complete example: ``` switch (animal) { case Animal.DOG, Animal.CAT => { // larger block expression // which spans multiple lines return "dry food"; } case Animal.TIGER, Animal.LION, Animal.CHEETA => { // larger block expression // which spans multiple lines return "fresh meat"; } case Animal.ELEPHANT => "hay"; default => { throw new Error("Unsupported Animal"); }; } ``` While you give examples that would totally work. Things that bother me about the approach are, when taken to something more complex than a quick value for value switch you end up with something that looks like this. ``` function houseAnimal() { // larger block expression // which spans multiple lines return "dry food"; } function wildCatFood() { // larger block expression // which spans multiple lines return "fresh meat"; } const cases = { [Animal.DOG]: houseAnimal, [Animal.CAT]: houseAnimal, [Animal.LION]: wildCatFood, [Animal.TIGER]: wildCatFood, [Animal.CHEETA]: wildCatFood, } const food = cases[animal] ? cases[animal]() : (() => {throw new Error("Unsuppored Animal")})(); ``` As we all know once any language reaches a basic level of functionality anything is possible. What I think is that JavaScript would benefit by having a cleaner approach.