In Scala I have the following trait:
trait Reactive {
type Condition = () => Boolean
type Reaction = (Condition) => Unit
var reactions = Map[Condition, Reaction]()
def addReaction(c: Condition, r: Reaction) { reactions += (c -> r) }
def addReactions(rs: List[Tuple2[Condition, Reaction]]) {
for(r <- rs) r match { case(condition, reaction) => addReaction(condition, reaction) }
}
def updateReactive() {
for(reaction <- reactions) reaction match {
case (c, r) => r(c)
}
}
}
then when I am trying to call the addReactions() method:
addReactions(List(
(() => UserInput.lastInput.isKeyHit('g'), () => (is: Boolean) => activateDevice(0))
))
I get the following error message on the second argument of the tuple:
- type mismatch; found : () => Boolean => Unit required: () => Boolean => Unit
I do not understand why. What do I have to do in order for the Reactive trait to store a set of boolean-conditioned functions that should be executed later, if their condition function returns true. Maybe I am going a round way? Is there any simpler approach?
Try writing this instead:
() => (is: Boolean) => activateDevice(0)is a function with no parameters that returns a function fromBooleantoUnit.condition => activateDevice(0)is a function with a single parameter calledcondition(whose type,() => Boolean, is inferred) and returnsUnit.