Hacker News new | ask | show | jobs
by hhandoko 3314 days ago
> Scala core library is littered with :: +: and other nonsensical operators.

They are very useful. With the two you mentioned (:: and +:) they can be used for pattern matching in addition to concatenation (of a list / sequence), for example:

    scala> val list = 1 :: 2 :: Nil
    list: List[Int] = List(1, 2)


    scala> val x = list match {
         |   case head :: _ => head
         |   case _         => 0
         | }
    x: Int = 1

    scala> val seq = 1 +: Seq(2, 3) :+ 4
    seq: Seq[Int] = List(1, 2, 3, 4)

    scala> val y = seq match {
         |   case a +: _ :+ b => a + b
         |   case _           => 0
         | }
    y: Int = 5
It can be a bit confusing at first, especially for mutable vs immutable collection operators. But you end up remembering some, if not most of it, after using them a number of times.
1 comments

and that's what I meant, when I said niche use cases. You can replace all operators with names and still have no difference. At some places code will be verbose, but on whole code will be more readable. Another example if you want is http://www.scala-lang.org/api/2.9.2/scala/sys/process/Proces.... All operators can be replaced by completely readable names like run, build, read, write.

Scala on whole is optimized to have lesser no. of characters in code, in contrast to verbosity of Java. Case classes and lambdas are what good came out of it, I understood the concept and now I can use them. Operator overloading and Implicit are what move pendulum to other side, they are not some novel concept and make very readable code, unreadable.

Why do we use operators at all? We can just as easily write out plus, minus, multiply, and divide.

Concatenating to a list and pattern matching are extremely frequent operations in Scala. Some of the operators they use have been in use for decades in the languages that influenced Scala.

Like many features across many languages it can be taken too far at times and people that enjoy Scala talk about them all the time. That said, the similarities of your example to operators I use at the command line every day makes it nice to read for me.

IMO the ProcessBuilder class is not a very good example, as the operators are designed to match the existing shell / bash ones (e.g. `#>>` is similar in behaviour to bash's `>>`).