Let alone why it's legal, why does this even return true
scala> class Bla {
def hello(x: Any): Boolean = x.toString.length == 2
}
defined class Bla
scala> new Bla().hello()
res0: Boolean = true
Running with -deprecation gives this
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
What the warning message means is:
hello()
is interpreted as hello(())
and since ().toString = "()"
, the method returns true
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With