- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Another confusing behavior
Wed, 2010-12-15, 15:44
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
Wed, 2010-12-15, 18:27
#2
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.
Wed, 2010-12-15, 18:37
#3
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:
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.
Wed, 2010-12-15, 18:47
#4
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.
Thu, 2010-12-16, 16:27
#5
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 C. Sobral
I travel to the future all the time.
| 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.
On Wed, Dec 15, 2010 at 12:41, Ittay Dror <ittay.dror@gmail.com> wrote:
--
Daniel C. Sobral
I travel to the future all the time.