Reputation: 4992
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?
Upvotes: 1
Views: 121
Reputation: 59994
Try writing this instead:
addReactions(List(
(() => UserInput.lastInput.isKeyHit('g'), condition => activateDevice(0))
))
() => (is: Boolean) => activateDevice(0)
is a function with no parameters that returns a function from Boolean
to Unit
. condition => activateDevice(0)
is a function with a single parameter called condition
(whose type, () => Boolean
, is inferred) and returns Unit
.
Upvotes: 4