- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
On the deprecation of the "-" method in the immutable List class
Mon, 2009-12-07, 01:36
Hi all,
in the scala.collection.immutable.List class you have deprecated the "-"
method in favor of using the "filterNot (_ == x)" expression. In my
humble opinion a method like "-" is needed in the immutable List (and in
all immutable collections in general) because the replacement is not
very intuitive for people new to Scala and to functional programming.
Currently the process a Scala newbie has to take in order to remove an
element from an immutable list is the following:
1. search for a method named "remove", "-" or similar
2. use the "-" method
3. compile and read the deprecation message
4. use the suggested "filterNot(_ == x)" expression
When at a later version the "-" method gets removed from the immutable
List class, a programmer new to Scala will have no obvious way to
discover the filterNot expression.
Please note that I am not stating that "filterNot(_ == x)" as an
expression is difficult or weird. The the problem here is not the
filterNot expression but the fact that most of us are used to removing
an element from a collection by just calling a method on the collection
and without the need to use a higher order function.
Best regards,
Spiros Tzavellas
Mon, 2009-12-07, 10:07
#2
Re: On the deprecation of the "-" method in the immutable List
On Mon, Dec 7, 2009 at 1:36 AM, Spiros Tzavellas wrote:
> Hi all,
>
> in the scala.collection.immutable.List class you have deprecated the "-"
> method in favor of using the "filterNot (_ == x)" expression. In my humble
> opinion a method like "-" is needed in the immutable List (and in all
> immutable collections in general) because the replacement is not very
> intuitive for people new to Scala and to functional programming.
>
> Currently the process a Scala newbie has to take in order to remove an
> element from an immutable list is the following:
> 1. search for a method named "remove", "-" or similar
> 2. use the "-" method
> 3. compile and read the deprecation message
> 4. use the suggested "filterNot(_ == x)" expression
>
Normally I would agree with you, but there's a snag: "-" did the
_wrong_ thing in 2.7.
It removed all elements equal to the argument in the list, not just
the first one. This is inconsistent with the behavior if "-" for other
collection classes. Unfortunately, we can't just fix "-", because
there's probably existing code relying on this behavior, and we do not
want to have applications fail in mysterious ways at runtime when
migrating to 2.8. So the best we can do is deprecate the method with
the old behavior now, drop it entirely in the next release, and
re-introduce with the correct behavior one release later. I know it's
painful, but I see no alternative.
Cheers
Mon, 2009-12-07, 12:27
#3
Re: On the deprecation of the "-" method in the immutable List
Hi Viktor,
of course you could create a wrapper but my point was the experience of
a person new to Scala. When you are learning the language, the absence
of a method that removes an element from a List might be confusing.
I also agree with you that "+" and "-" might not be great names for
collection methods.
Mon, 2009-12-07, 12:37
#4
Re: On the deprecation of the "-" method in the immutable List
Since this is the case, I agree with the deprecation of the "-" method.
you could always add a wrapper of your own with something like a "without"-method.
val l = List(1,2,3,4,5)
val l2 = l without 3
+ and - are not really sensible for sequences.
On Mon, Dec 7, 2009 at 1:36 AM, Spiros Tzavellas <sptz45@gmail.com> wrote:
--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall
Blog: klangism.blogspot.com
Twttr: twitter.com/viktorklang
Code: github.com/viktorklang