ryeguy
ryeguy

Reputation: 66851

What is the equivalent of this for loop as method calls?

Given this code:

for {
  evListeners <- eventListeners.get(manifest.erasure.asInstanceOf[Class[Any]])
  listener <- evListeners
} listener.asInstanceOf[A => Unit].apply(event)

How can I convert it to method calls? I tried this, but it throws an error while the above does not:

val listeners = eventListeners.get(manifest.erasure.asInstanceOf[Class[Any]])
listeners.foreach(_.asInstanceOf[A => Unit].apply(event))

Upvotes: 0

Views: 155

Answers (1)

Moritz
Moritz

Reputation: 14212

Assuming that eventListeners is Map[Class[Any],Seq[Any]] of some sort, you have to add one foreach call, as get on that map gives you a Option[Seq[Any]]:

val evListeners = eventListeners.get(manifest.erasure.asInstanceOf[Class[Any]])
evListeners.foreach(_.foreach(_.asInstanceOf[A => Unit].apply(event)))

Upvotes: 3

Related Questions