- About Scala
- In the Enterprise
- Scala Community
- Language Research
- In the Press
- The Scala Team
- Scala's Prehistory
- Contact Us
- Learning Scala
- Tour of Scala
- Scala API
- Setup & Getting Started
- Programming Guides
- Other Guides
- Code Examples
- Scala Developers
Inconsistency of operators
On Fri, Dec 2, 2011 at 11:08 AM, martin odersky wrote:
> Cay, thanks for the thoughtful suggestions!
> On Fri, Dec 2, 2011 at 7:55 PM, Cay Horstmann wrote:
>> And yes, the operators have gotten a bit out of hand in the collections library. I put them all into a table, and the result wasn't pretty. (The problem isn't the operators, but their inconsistency.) There is a lot to like about the collections library, but it's not perfect. Perhaps it needs to get redone one more time, or perhaps Scaladoc needs to be smarter about giving a more user-centric view.
> Do you have suggestions regarding inconsistency of operators? Given a long enough deprecation cycle we could do changes if they are necessary. One other thing I thought of was merging the Traversable and Iterable layers if collections, because this would get rid of about 20% of the supertraits of standard collection classes.
My gripe is with the operators to add and remove items. They are 17
operators + ++ :+ +: ++: :: ::: (for lists) | (for sets) += ++= +=:
++=: (for buffers) - -- &~ (for sets) -= --=
1) Why have ::? You can use +:, of course, but not always--in
particular, not in pattern matching.
2) Why the set operators? Just use ++, --
3) Is it really worth worrying about the difference between + and :+?
I understand that + is supposed to be used for unordered collections,
and :+ for ordered collections. But then shouldn't we also use ++ for
unordered and :++ for ordered? And shouldn't we use :+= instead of +=
for buffers? I think it would be simpler to say "+ and ++ add, and the
addition is at the end for ordered collections".
4) The +: and = don't work well together. += means "add/mutate". :+=
means "prepend/mutate". +:= should mean "append/mutate", but we need
the colon to the right, so it must be +=:. Or =+:? The choice seems
arbitrary. With :: for prepend, there is only one natural choice, =::
I realize it would be painful to eliminate :: for lists. So, I'd give
up on :+, and I'd give up on the distinction between + and +:. What's
+ ++ :: ::: - -- += ++= =:: =::: -= --=
That's 12 operators, one for each choice of
- one or many
- mutate or not
But if you love + vs :+ vs. +:, then the following 16 operators are a
+ ++ :+ :++ +: ++: - -- += ++= :+= :++= +=: ++=: -= --=