Does anyone know how to make something like this :...
# announcements
n
Does anyone know how to make something like this 🙂 ? I want to have type hints for reaction based on T class extended DomainEvent.
Copy code
val chainReactions: ChainReactions = mutableMapOf()
typealias ChainReactions = MutableMap<DomainEvent, DomainEvent>
1
r
reaction
is a lambda, but you seem to be treating it as a
DomainEvent
? Not sure what you're trying to do here...
Not sure if its what you're looking for but doing
reaction(event)
will solve your compile error.
b
(T: D) -> E
is not assignable to
(D) -> E
because, assuming that another type
Y
extends
D
, a function of type
(D) -> E
must accept a
Y
argument, which
(T) -> E
does not.
👆 1
In this case I would just do:
Copy code
chainReactions[event] = { d ->
   @Suppress("UNCHECKED_CAST")
   reaction(d as T)
}
assuming that
chainReactions
is encapsulated enough that the cast is guaranteed to be safe at runtime.
Also, if this is your real code, I believe you can omit the
E
parameter altogether, and replace references to it simply with
DomainEvent
.
Is it though? Because why is this function
inline
and using
reified
?
It would be nice to have a type system that can express map value types with some relation to the type of their specific key, but that doesn't exist (yet) 🙂
n
Of course. It was very late, I thing 🙂 The question was stupid 😛 I wanted to do something like this, of course:
Copy code
fun <T : DomainEvent> chainReaction(event: T, reaction: (T) -> DomainEvent) = apply {
        this.chainReactions[event] = reaction(event)
    }