What is the different between + and ++ applying on a set?
scala> val set = Set[String]("a","b")
set: scala.collection.immutable.Set[String] = Set(a, b)
scala> set + "c"
res2: scala.collection.immutable.Set[String] = Set(a, b, c)
scala> set ++ "c"
res3: scala.collection.immutable.Set[Any] = Set(a, b, c)
The first return Set[String] while the second return Set[Any].
It looks like ++ is more general, but what exactly is the ++ additional value?
If you look at the API doc for Set’s ++ method, you’ll see it takes a GenTraversableOnce
That means that in this case:
"c" is a GenTraversableOnce[Any], and then the ++ method adds all the elements of that collection.
A better example would be
(I suspect like many other answerers I just did a little fiddling in the Scala console to check and then double-check whether that’s through an implicit conversion or not. Yes it appears it is.)