Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

foreach and Enumeration

Tags:

scala

I'm try to learn some Scala reading Programming Scala, by Dean Wampler.

I'm trying to replicate a code snippet about Enumeration

object Breed extends Enumeration {
  val doberman = Value("Doberman Pinscher")
  val yorkie = Value("Yorkshire Terrier")
  val scottie = Value("Scottish Terrier")
  val dane = Value("Great Dane")
  val portie = Value("Portuguese Water Dog")
}

for (breed <- Breed) println(breed.id + "\t" + breed)

But, in the last line of code, I got this error:

value foreach is not a member of object Breed

Am I missing something? How can I solve?

like image 231
Aslan986 Avatar asked Mar 30 '13 10:03

Aslan986


1 Answers

You need to use .values:

for (breed <- Breed.values) println(breed.id + "\t" + breed)

And why not make it a bit more scala-y

Breed.values.foreach(breed => println(breed.id + "\t" + breed));
like image 72
Boris the Spider Avatar answered Sep 25 '22 17:09

Boris the Spider