Here is an example from book Programming in Scala
object Color extends Enumeration {
//val Red, Green, Blue = Value
val Red = Value("Red")
val Green = Value("Green")
}
for (d <- Color) print(d + " ") //Error value foreach is not a member of
// object xxx.Color
I have latest version of Scala. Is it the reason for error?
Enums don't have methods for iteration, like forEach() or iterator(). Instead, we can use the array of the Enum values returned by the values() method.
Can you loop through an enum C ++? Yes. It iterates over an std::initializer_list<Item>.
Enumeration is like an Iterator , not an Iterable . A Collection is Iterable . An Iterator is not.
This should be:
for (d <- Color.values) print(d + " ")
There used to be a foreach
method in Enumeration
, which is why doing just for (d <- Color)
worked. But it has been deprecated, and then removed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With