Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

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 
like image 522
Jagat Avatar asked Apr 12 '18 19:04

Jagat


1 Answers

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.

like image 72
Jagat Avatar answered Nov 03 '22 00:11

Jagat