开发者

Scala: why remove is deprecated in favor of filterNot?

开发者 https://www.devze.com 2023-03-14 13:59 出处:网络
scala> List(1, 2, 3) remove (_ < 2) <console>:8: warning: method remove in class List is deprecated: use `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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号