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

2.8 collections: containsSlice deprecated. why?

5 replies
Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.

scala> "foobar" containsSlice "oba"
:5: warning: method containsSlice in trait SequenceTemplate is deprecated: Should be repaced by indexOf(that) != -1
"foobar" containsSlice "oba"
^
res2: Boolean = false

having to test against -1 seems like a big step backwards to me.
if we can't have containsSlice back, how about at least using
Option[Int] instead of a -1 return?

(I just noticed that also, the answer is wrong! I opened a ticket on
that, http://lampsvn.epfl.ch/trac/scala/ticket/2186)

Ian Clarke
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: 2.8 collections: containsSlice deprecated. why?

On Wed, Jul 22, 2009 at 6:43 PM, Seth Tisue wrote:
> having to test against -1 seems like a big step backwards to me.
> if we can't have containsSlice back, how about at least using
> Option[Int] instead of a -1 return?

Indeed. I notice many other places in the standard API where
Option[A] should be returned, but instead an exception is thrown.

For example, List.head, List.last, List.tail - all throw exceptions in
the event of an empty list. Shouldn't they be returning Options?

Ian.

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: 2.8 collections: containsSlice deprecated. why?

Ian Clarke wrote:
> For example, List.head, List.last, List.tail - all throw exceptions in
> the event of an empty list. Shouldn't they be returning Options?
>
No, you're supposed to use pattern matching instead. :)

-0xe1a

Ian Clarke
Joined: 2008-12-18,
User offline. Last seen 42 years 45 weeks ago.
Re: 2.8 collections: containsSlice deprecated. why?

On Fri, Jul 24, 2009 at 1:17 PM, Alex Cruise wrote:
> Ian Clarke wrote:
>>
>> For example, List.head, List.last, List.tail - all throw exceptions in
>> the event of an empty list.  Shouldn't they be returning Options?
>>
>
> No, you're supposed to use pattern matching instead. :)

How do you pattern match for the last item in a list?

Ian.

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: 2.8 collections: containsSlice deprecated. why?
reverse and match on the head

On Sat, Jul 25, 2009 at 12:26 AM, Ian Clarke <ian.clarke@gmail.com> wrote:
On Fri, Jul 24, 2009 at 1:17 PM, Alex Cruise<alex@cluonflux.com> wrote:
> Ian Clarke wrote:
>>
>> For example, List.head, List.last, List.tail - all throw exceptions in
>> the event of an empty list.  Shouldn't they be returning Options?
>>
>
> No, you're supposed to use pattern matching instead. :)

How do you pattern match for the last item in a list?

Ian.

--
Ian Clarke
CEO, Uprizer Labs
Email: ian@uprizer.com
Ph: +1 512 422 3588
Fax: +1 512 276 6674

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: 2.8 collections: containsSlice deprecated. why?

On Friday July 24 2009, Ian Clarke wrote:
> On Fri, Jul 24, 2009 at 1:17 PM, Alex Cruise wrote:
> > Ian Clarke wrote:
> >> For example, List.head, List.last, List.tail - all throw
> >> exceptions in the event of an empty list.  Shouldn't they be
> >> returning Options?
> >
> > No, you're supposed to use pattern matching instead. :)
>
> How do you pattern match for the last item in a list?

someList match {
case Nil => // someList is empty
case last :: Nil => // last is the last element
case first :: more => // first is the head, more is a non-empty remainder
}

> Ian.

Randall Schulz

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