# Type error not detected in "contains" method

**URL:** <https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992>\
**Category:** Libraries\
**Created:** [May 15, 2024, 9:08pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992 "2024-05-15T21:08:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jrlemieux](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jrlemieux/32/2995_2.png) [@jrlemieux](https://users.scala-lang.org/u/jrlemieux)\
**Post date:** [May 15, 2024, 9:08pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/1 "2024-05-15T21:08:37Z")

</div>

I have this test code:

```auto
object Main:

  trait X0
  class X1(name: String) extends X0
  class X2(name: String) extends X1(name)

  {
    val seq = Seq[X1]()

    seq.contains(3.1415926) //compiles
    seq.contains("a") //compiles
    seq.contains(X1("a"): X0) //compiles
    seq.contains(X1("a"))
    seq.contains(X2("a"))
  }

  {
    case class MySeq[A]():
      def contains[A1 <: A](a: A1): Boolean = false

    val seq = MySeq[X1]()

    seq.contains(3.1415926) // does not compile
    seq.contains("a") // does not compile
    seq.contains(X1("a"): X0) // does not compile
    seq.contains(X1("a"))
    seq.contains(X2("a"))
  }

```

The signature of “contains” of Seq is:

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

Beause of this, checking for the existence of a double, a string, or type X0 is allowed.

If the signature was changed to:

def contains[A1 \<: A](elem: A1): Boolean

as I do in the MySeq class, then testing with these invalid types is not possible, the code does not compile, which seems like the proper behavior.

Is this a bug?

Thank you.

---

<div class="post-metadata">

**Author:** ![spamegg1](https://users.scala-lang.org/user_avatar/users.scala-lang.org/spamegg1/32/2131_2.png) [@spamegg1](https://users.scala-lang.org/u/spamegg1)\
**Post date:** [May 15, 2024, 9:27pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/2 "2024-05-15T21:27:27Z")

</div>

I don’t think it’s a bug; it’s a [design decision of the `Seq` class](https://scala-lang.org/api/3.x/scala/collection/SeqOps.html#contains-e1c) I think (to have `A1 >: A`). It’s due to covariance as BalmungSan mentioned.

Variance of collections is a design choice; for example in Java, `Array` is treated covariant, which leads to issues in the type system when combined with mutation. In Scala, immutable collections like `List`, `Seq`, `ArraySeq` etc. are covariant, mutable ones like `Array`, `ArrayBuffer` are invariant to avoid those issues. ([Programming in Scala 5th Edition](https://docs.scala-lang.org/books.html) for further reading)

---

<div class="post-metadata">

**Author:** ![BalmungSan](https://users.scala-lang.org/user_avatar/users.scala-lang.org/balmungsan/32/1700_2.png) [@BalmungSan](https://users.scala-lang.org/u/BalmungSan)\
**Post date:** [May 15, 2024, 9:31pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/3 "2024-05-15T21:31:16Z")

</div>

No, is not a bug, it is _“working as expected”_.

Since the real `Seq` is covariant _(contrary to your `MySeq`)_ then the signature you propose is invalid due to Liskov.

A simple way to understand why that is the case is the following:

```scala
val numbers: Seq[Int] = Seq(1, 2, 3)
val things: Seq[Any] = numbers // Valid due to covariance.
things.contains("foo") // Valid due subtyping.

```

Since the previous code MUST compile, then `Seq.contains` MUST already support `Any` type.

* * *

A common way to avoid most instances of this mistake is using a lint warning everytime the compiler infers `Any`.

You may also use a more restrictive version of contains based on a `Eq` typeclass provided by **cats**.

* * *

Another discussion would be if `Seqs` should be covariant or not, or if subtyping was a good idea or not. But those two would lead to very different programming languages than **Scala**.

---

<div class="post-metadata">

**Author:** ![jrlemieux](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jrlemieux/32/2995_2.png) [@jrlemieux](https://users.scala-lang.org/u/jrlemieux)\
**Post date:** [May 15, 2024, 9:43pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/4 "2024-05-15T21:43:56Z")

</div>

Many thanks. I’ve been using Scala for many years, and I just realized that Seq was covariant. I am very surprised, as this can lead to a great deal of undetected programming errors. If all Scala collections are like this, I would say that we have a huge issue. If I want the behavior expressed in my “MySeq” type, should I use cats as you mentioned? Thanks! J

---

<div class="post-metadata">

**Author:** ![alex.boisvert](https://users.scala-lang.org/user_avatar/users.scala-lang.org/alex.boisvert/32/2224_2.png) [@alex.boisvert](https://users.scala-lang.org/u/alex.boisvert)\
**Post date:** [May 15, 2024, 10:01pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/5 "2024-05-15T22:01:12Z")

</div>

You may also want to read through the “multiversal equality” document to learn more about equality in general and how Scala now offers a more sophisticated mechanism to compare for equality.

[https://docs.scala-lang.org/scala3/reference/contextual/multiversal-equality.html](https://docs.scala-lang.org/scala3/reference/contextual/multiversal-equality.html)

---

<div class="post-metadata">

**Author:** ![som-snytt](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@som-snytt](https://users.scala-lang.org/u/som-snytt)\
**Post date:** [May 15, 2024, 10:15pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/6 "2024-05-15T22:15:48Z")

</div>

Demonstrating the lint (thanks BalmungSan):

```auto
$ scala -Xlint
Welcome to Scala 2.13.14 (OpenJDK 64-Bit Server VM, Java 21.0.2).
Type in expressions for evaluation. Or try :help.

scala> "abc".contains("b")
val res0: Boolean = true

scala> "abc".contains('b'.toInt)
             ^
       warning: a type was inferred to be `AnyVal`; this may indicate a programming error.
val res1: Boolean = true

scala> "abc".contains('b')
val res2: Boolean = true

scala>

```

Just a moment ago, I avoided `contains` on `String` and just used `indexOf` which is unproblematic. It turned out I needed the index anyway, in order to detect last position for this behavior:

```auto
scala> "ab".split('b')
val res3: Array[String] = Array(a)

```

It looks like Scala 3 does not have the lint yet.

---

<div class="post-metadata">

**Author:** ![BalmungSan](https://users.scala-lang.org/user_avatar/users.scala-lang.org/balmungsan/32/1700_2.png) [@BalmungSan](https://users.scala-lang.org/u/BalmungSan)\
**Post date:** [May 15, 2024, 10:17pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/7 "2024-05-15T22:17:02Z")

</div>

> [@jrlemieux](#):
>
> and I just realized that Seq was covariant

Well, note that usually, this is what you would expect since follows the _“natural”_ order of subtyping.  
A `List` of `Dogs` is a `List` of `Pets`.

> [@jrlemieux](#):
>
> I am very surprised, as this can lead to a great deal of undetected programming errors. If all Scala collections are like this, I would say that we have a huge issue.

Well yes, but not really.  
First, as mentioned before, the lint for _“infer `Any`”_ will catch most instances of this mistake.  
Second, good testing would usually catch the remaining instances.  
Third, folks are usually already conditioned to look for this, this is not different from `1 == "foo"`.

Now, having said that, yeah I agree it would be great if the type system could catch this.  
But this is just a consequence of subtyping.

> [@jrlemieux](#):
>
> If I want the behavior expressed in my “MySeq” type, should I use cats as you mentioned?

Yes, you may use `contains_` from **cats**.  
Eventually, the stdlib may support something similar, as @alex.boisvert mentioned; but that would need a couple of years.

---

<div class="post-metadata">

**Author:** ![jrlemieux](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jrlemieux/32/2995_2.png) [@jrlemieux](https://users.scala-lang.org/u/jrlemieux)\
**Post date:** [May 15, 2024, 10:26pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/8 "2024-05-15T22:26:47Z")

</div>

I changed the code with:

trait X0 derives CanEqual

And added these options in SBT:

scalacOptions ++= Seq(  
“-language:strictEquality”,  
“-Xlint:infer-any”  
)

And when I compile the code, all calls to “contains” are accepted, no warnings, no errors.

Does it mean that there is no way to detect this issue on the standard “contains” methods of the Scala collections?

Thank you to everyone.

---

<div class="post-metadata">

**Author:** ![jrlemieux](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jrlemieux/32/2995_2.png) [@jrlemieux](https://users.scala-lang.org/u/jrlemieux)\
**Post date:** [May 15, 2024, 11:21pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/9 "2024-05-15T23:21:35Z")

</div>

I downloaded the Scala3 project, and the string “infer-any” does not appear in the code. So Xlint is just ignored.

Any way to make “contains” safer for Scala 3?

---

<div class="post-metadata">

**Author:** ![jrlemieux](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jrlemieux/32/2995_2.png) [@jrlemieux](https://users.scala-lang.org/u/jrlemieux)\
**Post date:** [May 15, 2024, 11:22pm UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/10 "2024-05-15T23:22:06Z")

</div>

This does not work for Scala 3.

---

<div class="post-metadata">

**Author:** ![som-snytt](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@som-snytt](https://users.scala-lang.org/u/som-snytt)\
**Post date:** [May 16, 2024, 1:16am UTC](https://users.scala-lang.org/t/type-error-not-detected-in-contains-method/9992/11 "2024-05-16T01:16:40Z")

</div>

> [@som-snytt](#):
>
> It looks like Scala 3 does not have the lint yet.

😿
