Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Scala's Addable deprecated?

Tags:

scala

I noticed that Addable is deprecated, while Subtractable is not. What's wrong with Addable, and why is Subtractable different?

like image 433
Daniel Lubarov Avatar asked Jun 18 '11 00:06

Daniel Lubarov


2 Answers

Expanding on Daniel's answer, + is also a very bad operator to use for collection insertion. Mathematically, the + operator has a very conventional meaning, and part of that meaning is a guarantee of associativity. Unfortunately, associativity is a guarantee that doesn't make any sense at all when you're adding an Int to a Vector[Int]. As such, + was always a very confusing operator for anyone who had any algebraic training.

+: and :+ are superior in several ways, not the least of which is that there is no expectation of associativity. In fact, the very asymmetry of the operators imply non-associativity, which is precisely their semantics. Also +: and :+ mirror each-other very nicely, and +: is right-associative, all of which conspires to provide a very natural API for collection prepend and append, respectively.

like image 93
Daniel Spiewak Avatar answered Nov 11 '22 04:11

Daniel Spiewak


The problem is that + is overloaded to concatenate String to non-strings. So, whenever you use the + method on a type that doesn't have it, you'll get an error message that is not related to the real problem: that the type you have isn't the one you expected.

There's +: and :+ to replace it.

like image 42
Daniel C. Sobral Avatar answered Nov 11 '22 06:11

Daniel C. Sobral