gpt4 book ai didi

scala - 为什么在没有任何参数的情况下调用采用 Any 的方法是合法的?

转载 作者:行者123 更新时间:2023-12-04 14:54:23 27 4
gpt4 key购买 nike

更不用说为什么它是合法的,为什么这甚至返回 true

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

关于scala - 为什么在没有任何参数的情况下调用采用 Any 的方法是合法的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49804757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com