开发者

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)))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜