Strange pattern matching behaviour with AnyRef

做~自己de王妃 提交于 2019-12-04 01:32:59

This is because Any is actually just an Object. Having Double there is a convenient fiction--it's actually java.lang.Double which is autounboxed for you in the match statement. Unfortunately, there is no way for Scala to tell if it finds a java.lang.Double if it is supposed to be interpreted as a Double or as a java.lang.Double--in the latter case, the AnyRef should catch it. So it does. But if you specifically ask for a Double, it knows it is supposed to unbox, and then the AnyRef case need not be checked. (And, in fact, if you intended it to be a java.lang.Double, it will unbox that too--it can't tell the difference.)

Whether this is ideal behavior is debatable, but it is logical.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!