This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Another confusing behavior

5 replies
Ittay Dror 2
Joined: 2010-05-05,
User offline. Last seen 42 years 45 weeks ago.

scala> val a = Array("hi")
a: Array[java.lang.String] = Array(hi)

scala> a.contains(Some("hi"))
res23: Boolean = false

Looking at the signature, contains expects an Any (this is for all
SeqLike collections). Why is that? (If I want to find elements of type B
using an object of type A I can use `exists`)

Regards,

Ittay

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Another confusing behavior
Seq uses Any as input for contains because Seq is co-variant. Compare that to Set, which an have A as type of contains because it is invariant.

On Wed, Dec 15, 2010 at 12:41, Ittay Dror <ittay.dror@gmail.com> wrote:
scala> val a = Array("hi")
a: Array[java.lang.String] = Array(hi)

scala> a.contains(Some("hi"))
res23: Boolean = false


Looking at the signature, contains expects an Any (this is for all SeqLike collections). Why is that? (If I want to find elements of type B using an object of type A I can use `exists`)


Regards,

Ittay




--
Daniel C. Sobral

I travel to the future all the time.
Ittay Dror 2
Joined: 2010-05-05,
User offline. Last seen 42 years 45 weeks ago.
Re: Another confusing behavior


Daniel Sobral wrote:
AANLkTikt2NHuLZuLm0raKdprL6FzjH4wYt0nvVE-8tjR [at] mail [dot] gmail [dot] com" type="cite">Seq uses Any as input for contains because Seq is co-variant. Compare that to Set, which an have A as type of contains because it is invariant.

I don't understand. How can contains of Seq[A] return true for anything that is not an A (including subclasses of course). Seq(1,2,3).contains("hi") looks illogical to me

AANLkTikt2NHuLZuLm0raKdprL6FzjH4wYt0nvVE-8tjR [at] mail [dot] gmail [dot] com" type="cite">
On Wed, Dec 15, 2010 at 12:41, Ittay Dror <ittay [dot] dror [at] gmail [dot] com" rel="nofollow">ittay.dror@gmail.com> wrote:
scala> val a = Array("hi")
a: Array[java.lang.String] = Array(hi)

scala> a.contains(Some("hi"))
res23: Boolean = false


Looking at the signature, contains expects an Any (this is for all SeqLike collections). Why is that? (If I want to find elements of type B using an object of type A I can use `exists`)


Regards,

Ittay




--
Daniel C. Sobral

I travel to the future all the time.
Adam Rabung
Joined: 2010-04-23,
User offline. Last seen 42 years 45 weeks ago.
Re: Another confusing behavior
i asked this same thing on SO, got a good answer:http://stackoverflow.com/questions/2078246/why-does-seq-contains-accept-type-any-rather-than-the-type-parameter-a/2078619#2078619

On Wed, Dec 15, 2010 at 12:17 PM, Ittay Dror <ittay.dror@gmail.com> wrote:


Daniel Sobral wrote:
Seq uses Any as input for contains because Seq is co-variant. Compare that to Set, which an have A as type of contains because it is invariant.

I don't understand. How can contains of Seq[A] return true for anything that is not an A (including subclasses of course). Seq(1,2,3).contains("hi") looks illogical to me


On Wed, Dec 15, 2010 at 12:41, Ittay Dror <ittay.dror@gmail.com> wrote:
scala> val a = Array("hi")
a: Array[java.lang.String] = Array(hi)

scala> a.contains(Some("hi"))
res23: Boolean = false


Looking at the signature, contains expects an Any (this is for all SeqLike collections). Why is that? (If I want to find elements of type B using an object of type A I can use `exists`)


Regards,

Ittay




--
Daniel C. Sobral

I travel to the future all the time.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Another confusing behavior

On Wed, Dec 15, 2010 at 07:17:18PM +0200, Ittay Dror wrote:
> I don't understand. How can contains of Seq[A] return true for
> anything that is not an A (including subclasses of course).
> Seq(1,2,3).contains("hi") looks illogical to me

def f(x: Seq[Any]) = x contains "a"

f(List(1, 2))
f(List("a", "b"))

If contains doesn't take "Any" you can't even call the method here.
This is variance in action.

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Another confusing behavior
scala> class C[X](xs: X*) {
     |   def contains(x: X) = xs contains x
     | }
defined class C

scala> class D[+X](xs: X*) {              
     |   def contains(x: X) = xs contains x
     | }                                  
<console>:22: error: covariant type X occurs in contravariant position in type X of value x
         def contains(x: X) = xs contains x
                      ^


On Wed, Dec 15, 2010 at 15:17, Ittay Dror <ittay.dror@gmail.com> wrote:


Daniel Sobral wrote:
Seq uses Any as input for contains because Seq is co-variant. Compare that to Set, which an have A as type of contains because it is invariant.

I don't understand. How can contains of Seq[A] return true for anything that is not an A (including subclasses of course). Seq(1,2,3).contains("hi") looks illogical to me


On Wed, Dec 15, 2010 at 12:41, Ittay Dror <ittay.dror@gmail.com> wrote:
scala> val a = Array("hi")
a: Array[java.lang.String] = Array(hi)

scala> a.contains(Some("hi"))
res23: Boolean = false


Looking at the signature, contains expects an Any (this is for all SeqLike collections). Why is that? (If I want to find elements of type B using an object of type A I can use `exists`)


Regards,

Ittay




--
Daniel C. Sobral

I travel to the future all the time.



--
Daniel C. Sobral

I travel to the future all the time.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland