Why is Scala's Addable deprecated?
开发者_JS百科I noticed that Addable
is deprecated, while Subtractable
is not. What's wrong with Addable
, and why is Subtractable
different?
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.
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.
精彩评论