Scala Flags Equivalent

Is there an equivalent for flags in Scala?

Sort of:

val flags = Something | SomeOtherThing

I think I could make a class for each set of flags and have a bunch of Boolean ones, but what about syntactic sugar?

+4
source share
3 answers

To a large extent, this depends on what you are trying to accomplish.

If you interact with legacy code (especially one that you cannot change) and / or need super-super-high performance ( you usually don’t use it ), the canonical way is to create a type alias forInt . Ie, as in the example of the linked library:

type MyFlags = Int
...
val theseFlags: MyFlags

Int , "" - - "" .

Scala - (ab) BitSet ( ).


"-" , Java enum/EnumSet combo, Enumeration ValueSet. , doc:

scala>   object WeekDay extends Enumeration {
     |     type WeekDay = Value
     |     val Mon, Tue, Wed, Thu, Fri, Sat, Sun = WeekDay
     |   }
defined module WeekDay

scala>   import WeekDay._
import WeekDay._

scala> var newSet = ValueSet.empty
newSet: WeekDay.ValueSet = WeekDay.ValueSet()

scala> newSet += Mon

scala> newSet
res1: WeekDay.ValueSet = WeekDay.ValueSet(Mon)

scala> newSet & WeekDay.values
res2: WeekDay.ValueSet = WeekDay.ValueSet(Mon)

ValueSet Value - . , Java EnumSet. Scala , Int - type, Value ( ).

.


, . , Int s.

, , - , Enumeration . , , ValueSet BitSet.

+7

Something SomeOtherThing , .

, :

val flags : Int = Something | SomeOtherThing

Scala , Java. Google, .

+3

Weekday.type Weekday.Value, mikolak.

Update

val Mon, Tue, Wed, Thu, Fri, Sat, Sun = WeekDay

val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
+1

All Articles