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(_.asInstan开发者_JS百科ceOf[A => Unit].apply(event))
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)))
精彩评论