"why is the + operator for list deprecated in scala?" Code Answer


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 call reverse; or use a listbuffer, a mutable list that does offer an append operation, and when you're done call tolist.

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 using add(), and then had second thoughts about it.

By 23ars on June 8 2022

Answers related to “why is the + operator for list deprecated in scala?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.