繁体   English   中英

为什么在没有任何参数的情况下调用一个接受Any的方法是合法的?

[英]Why is it legal to call a method that takes Any without any argument?

更不用说为什么它是合法的,为什么这甚至会回归真实

scala> class Bla { 
    def hello(x: Any): Boolean = x.toString.length == 2 
}
defined class Bla

scala> new Bla().hello() 
res0: Boolean = true 

使用-deprecation运行会给出这个

scala> scala> class Bla { 
  def hello(x: Any): Boolean = x.toString.length == 2 
}
defined class Bla

scala> new Bla().hello()
<console>:13: warning: Adaptation of argument list by inserting () has been deprecated: leaky (Object-receiving) target makes this especially dangerous.
        signature: Bla.hello(x: Any): Boolean
  given arguments: <none>
 after adaptation: Bla.hello((): Unit)
       new Bla().hello()
                      ^
res0: Boolean = true

警告信息的含义是:

hello()被解释为hello(())和since ().toString = "()" ,该方法返回true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM