Scala 3: How to loop/iterate over enum values in a 'for' loop

If for some reason you ever need to iterate over the values in a Scala 3 enum, I can confirm that this approach works:

// [1] create an enum
enum CrustSize:
    case Small, Medium, Large

// [2] loop over its elements/values
for e <- CrustSize.values do println(e)

If you put that code in the Scala REPL, you’ll see that it prints out the enum values, Small, Medium, and Large.

Calling CrustSize.values returns an Array[CrustSize], so once you’ve made this call, you can treat a Scala 3 enum just like any other sequence.

In summary, if you ever need to iterate over a Scala 3 enum, I hope this example is helpful.