Why is the + operator for List deprecated in Scala?
http://www.scala-lang.org/docu/files/api/scala/List.html#%2B%28B%29
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Good question, so I looked it up in the book by Odersky et al. It says the following (hopefully it’s not a copyright infringement to quote this here ;-)):
Why not append to lists?
Class
Listdoes not offer an append operation because the time it takes to append to a list grows linearly with the size of the list, whereas prepending with::takes constant time. Your options if you want to build a list by appending elements is to prepend them, then when you’re done callreverse; or use aListBuffer, a mutable list that does offer an append operation, and when you’re done calltoList.As far as I understand FP, prepending to a list is much more common than appending, at least in pure functional languages. I can only assume that the designers of Scala added the
+operator as a convenience for Java developers, who are used to appending usingadd(), and then had second thoughts about it.