Scala: why remove is deprecated in favor of filterNot?
scala> List(1, 2, 3) remove (_ < 2)
<console>:8: warning: method remove in class List is deprecated: use `filterNot'开发者_开发知识库
instead
List(1, 2, 3) remove (_ < 2)
^
res0: List[Int] = List(2, 3)
I don't understand why this is deprecated. Being immutable it should be clear that remove
would return a new list. In scaladoc you can find only:
Deprecated: use filterNot' instead
It's because the method remove
wasn't coherent - for some collections it did a mutable in-place removal, whereas for immutable collections it created a new version. Methods with in-place (bulk) modifications should only be available for mutable collections.
精彩评论