Scala bitwise-like method argument
Let's say there is a generic method declaration that performs a set of
operations based upon a designated mode that would look something like
this:
def doSomethingSmart(mode: OpMode, someGoodyList: List[Any]): Boolean = {
/* do foo */ }
Where OpMode is a type/enumeration consisting of:
Read
Create
Delete
Modify
Whatever
Putting the two together would obviously yield a single-mode, reusable,
code block.
Now, the type/enumeration part would probably look something like this:
object OpMode extends Enumeration {
type OpMode = Value
val Read, Write, Create, Modify, Delete, Whatever = Value
}
But let's say you wanted to expand the scope of doSomethingSmart() to span
what is typically done using bitwise operators, for example: Create &
Modify & Whatever. Is there a "scala-way" of restricting the bit-masked
argument to that limited data-set (ie, the enumeration/type), or is it
best to simply drop back to binary-indexed value assignments - in which
cases there is no "type" checking per se? Maybe Something that might
resemble this:
def doSomethingSmarter(more: T < [MaskOf[OpMode]], ...
TIA.
EDIT: I guess another possibility would be to change the OpMode to be a
List and the run a series of "contains" operations...
No comments:
Post a Comment