Scala inconveniently contains behavior
I was writing a simple application using Scala and I noticed some rather strange behavior. I managed to call the contains method of the String class and pass any object to it. Here's some simple code to illustrate it. I used a worksheet and I don't think there is a need to write the main method as it doesn't matter.
class Man
val m = new Man
"hello".contains(m)
I was very surprised that the Scala compiler didn't complain and returned false. Therefore, I decided to study the contents in more detail.
First, the String class itself does not have a contains method. It is located in the StringOps class, which, as far as I know, String can be implicitly converted. The method looks like this: Scala 2.11 docs:
def contains [A1>: Char] (elem: A1): Boolean
Checks if this sequence contains the specified value as an element.
Therefore, since I understand type boundaries, the element must be of supertype Char. The question is, how can it be that the class Man is a supertype of Char? Is there any implicit conversion? I noticed that 2.10 Scala docs contain another definition contains:
def contains (elem: Any): Boolean Checks if this string contains the given value as an element.
This method looks pretty logical to me since the Man class is also clearly set to Any. However, the most recent documentation I have found contains the definition that I provided earlier.
source to share