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

时间:2018-04-12 19:47:31

标签: scala

更别说为什么它是合法的,为什么这甚至会回归

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

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

1 个答案:

答案 0 :(得分:6)

使用-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(())().toString = "()"开始,该方法返回true