Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Long/Int value of a enum set in Scala 2.8

In Scala 2.7, Enumeration provide Set32/Set64 to build enum set and easily get the bitwise value in Long/Int or build enum set back from a Long/Int value (which ease db storage). Scala 2.8 removed these classes. Is there a replacement in 2.8 lib?

like image 219
Jarod Avatar asked Nov 06 '22 11:11

Jarod


1 Answers

The representation is quite easy to implement and you should do this yourself as the internal implementation of the Scala libs may change and your data would be broken:

object WeekDay extends Enumeration {
     type WeekDay = Value
     val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
import WeekDay._
val values = Set(Mon,Wed,Fri)
require(values.length < 31)
(0 /: values) ((s, c) => s | 1 << c.id).toBinaryString

Keep in mind that storing the ordinal in the database is fragile: Storing EnumSet in a database?. You should pick a stabile ordinal for each value.

like image 74
Thomas Jung Avatar answered Nov 15 '22 05:11

Thomas Jung