List (1) #contains () returns `false`

Considering the following:

scala> List(1).contains()
warning: there was one deprecation warning; re-run with -deprecation for details
res18: Boolean = false

      

Why is it coming back false

?

List # contains has the signature:

def contains[A1 >: A](elem: A1): Boolean

So, as I understand it, the argument contains

must be equal or higher A

.

Why does it return false

?

+3


source to share


1 answer


If you run it again with -deprecation

(as mentioned in the warning), you will see the following:

scala> List(1).contains()
<console>:8: warning: Adaptation of argument list by inserting ()
  has been deprecated: this is unlikely to be what you want.
        signature: LinearSeqOptimized.contains[A1 >: A](elem: A1): Boolean
  given arguments: <none>
 after adaptation: LinearSeqOptimized.contains((): Unit)
              List(1).contains()
                              ^
res0: Boolean = false

      



So, List(1).contains()

parsed as List(1).contains(())

, and the inferred type for A1

- AnyVal

, which is the smallest upper bound of Unit

and Int

.

The short answer is: don't do it, it's bad. A bit longer: don't do it, it's bad, and if the compiler suggests to rerun with -deprecation

, take it in the sentence - it might make something even clearer.

+7


source







All Articles