You can use an Enumeration
, and use its ValueSet
to store your true values instead of individual var
s. This lets you refer to them by String
name:
object MyBoolValue extends Enumeration {
type MyBoolValue = Value
val Ma, Co, Ar, Le, St, Ri = Value
}
class MyBoolValueContext {
var trueValues = MyBoolValue.ValueSet.empty
def isTrue(v: MyBoolValue) = trueValues contains v
def apply(v: MyBoolValue) = isTrue(v)
}
So then you can do:
import MyBoolValue._
val c = new MyBoolValueContext
c(Ma)
c.trueValues += Le
c.trueValues -= St
def logic: Boolean = (c(Ma) &
(!c(Ma) | c(Co)) &
(!c(Ma) | c(Ar)) &
(!c(Co) | c(Ma)) &
(!c(Ar) | c(Ma)) &
(!c(Ar) | c(Le) | c(St) | c(Ri)))
And you can use withName
to handle String
input:
c.trueValues ++= List("Ar", "Le", "St").map(MyBoolValue.withName)
You can get a little fancier by making the context implicit:
implicit case class ResolveMyBoolValue(self: MyBoolValue) extends AnyVal {
def get(implicit context: MyBoolValueContext): Boolean = context.isTrue(self)
}
implicit val context = new MyBoolValueContext
val result = Ar.get | !St.get
Or use an implicit conversion, though this could cause some confusion if misused:
implicit def resolveMyBoolValue(v: MyBoolValue)
(implicit context: MyBoolValueContext) = {
context.isTrue(v)
}
val result: Boolean = Le