lastIndexOf and lastIndexOfSlice

196 views
Skip to first unread message

Grzegorz Balcerek

unread,
Mar 16, 2011, 4:24:16 AM3/16/11
to scala...@googlegroups.com
Hello,
 
I noticed that the following expressions give different results:
 
Seq(1,2,3,4,5).lastIndexOf(5,4)
Seq(1,2,3,4,5).lastIndexOfSlice(Seq(5),4)
 
and that the following two give the same result
 
Seq(1,2,3,4,5).lastIndexOf(5)
Seq(1,2,3,4,5).lastIndexOfSlice(Seq(5))
(Scala 2.8.1)
 
Is that normal? Is it the expected outcome?
 
scala> Seq(1,2,3,4,5).lastIndexOf(5,4)
res0: Int = 4
 
scala> Seq(1,2,3,4,5).lastIndexOfSlice(Seq(5),4)
res1: Int = -1
 
scala> Seq(1,2,3,4,5).lastIndexOf(5)
res2: Int = 4
 
scala> Seq(1,2,3,4,5).lastIndexOfSlice(Seq(5))
res3: Int = 4
 
Regards,
Grzegorz Balcerek
 
 

Seth Tisue

unread,
Mar 16, 2011, 8:16:33 AM3/16/11
to scala...@googlegroups.com
>>>>> "Grzegorz" == Grzegorz Balcerek <gbal...@echostar.pl> writes:

Grzegorz> Hello, I noticed that the following expressions give
Grzegorz> different results:

Grzegorz> Seq(1,2,3,4,5).lastIndexOf(5,4)
Grzegorz> Seq(1,2,3,4,5).lastIndexOfSlice(Seq(5),4)

Grzegorz> Is that normal? Is it the expected outcome?

I'd say open a ticket.

--
Seth Tisue @ Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/

Fabio Cechinel Veronez

unread,
Mar 16, 2011, 8:57:47 AM3/16/11
to Seth Tisue, scala...@googlegroups.com
Another question regarding indexOf method:

Welcome to Scala version 2.8.1.final (Java HotSpot(TM) Server VM, Java
1.6.0_21).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val list: List[Int] = List.range(1,10)
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> list.indexOf("test")
res1: Int = -1

I expected a compilation error when calling indexOf with a String
since its parameter is of type A of List[A].

Am I missing something?

--
Fabio Cechinel Veronez

Nate Nystrom

unread,
Mar 16, 2011, 9:03:38 AM3/16/11
to Fabio Cechinel Veronez, Seth Tisue, scala...@googlegroups.com
The signature of indexOf is:

def indexOf [B >: A] (elem: B) : Int

The compiler is inferring Any for B.

Nate

Paul Phillips

unread,
Mar 16, 2011, 9:07:05 AM3/16/11
to Fabio Cechinel Veronez, Seth Tisue, scala...@googlegroups.com
On 3/16/11 5:57 AM, Fabio Cechinel Veronez wrote:
> scala> val list: List[Int] = List.range(1,10)
> list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9)
>
> scala> list.indexOf("test")
> res1: Int = -1
>
> I expected a compilation error when calling indexOf with a String
> since its parameter is of type A of List[A].
>
> Am I missing something?

You are missing variance. It's the same reason contains takes an Any.
A covariant collection cannot accept invariant parameters.

Grzegorz Balcerek

unread,
Mar 17, 2011, 9:20:10 AM3/17/11
to scala...@googlegroups.com

----- Original Message -----
From: "Seth Tisue" <se...@tisue.net>
> I'd say open a ticket.

I created the following ticket.

https://lampsvn.epfl.ch/trac/scala/ticket/4348

Grzegorz Balcerek

Reply all
Reply to author
Forward
0 new messages