mardi 17 mai 2016

Scala: Create enumeration object from a set

I would like to know whether there is any way to create an enumeration class from a set (e.g., a set of strings).

Below is a simple attempt:

val s = Set("v1", "v2", "v3")

object sEnum extends Enumeration {
  for (v <- s) {
    val v = Value
  }
}

for (v <- sEnum.values) {
  println(v)
}

The enumeration object seems to compile, but it's values are wonky: the print loop shows that the values inside sEnum look like this:

<Invalid enum: no field for #0>

Thus, the problem seems to be at the value declaration

val v = Value

Is there some way that I can replace the variable v by its contents at run time (using reflection I assume)?





Aucun commentaire:

Enregistrer un commentaire