docs.scala-lang icon indicating copy to clipboard operation
docs.scala-lang copied to clipboard

"Generic Classes" page in Tour of Scala uses an unnecessarily complicated example

Open knight-of-zero opened this issue 4 years ago • 1 comments

I'm new to scala, but have a deep background in Java. I've been going through the "tour of scala" to learn the language. Overall this guide is super helpful and well written... but I did hit one snag on the Generic Classes page:

  def push(x: A): Unit =
    elements = x :: elements

At first I assumed :: was some kind of built-in part of the language (like + on ints or strings). Eventually I hit the Operators page which set me straight and described infix notation.

This was puzzling, though. If infix notation still goes {object} {op} {argument}, does that mean Int has a def ::(to: List[Int]) on it? That seems a bit crazy given that List is generic, so you'd have to add :: to every type in the stdlib to make things consistent.

I went down quite a rabbit hole (1-2 hours?) googling and running little experiments to try to figure out what was going on. I eventually found my way to the language spec, which briefly mentioned:

The associativity of an operator is determined by the operator's last character. Operators ending in a colon ‘:’ are right-associative. All other operators are left-associative.


All that said, I have two suggestions to improve the tour:

  1. Avoid method names that end in colons throughout the other examples. The extra complexity just distracts from the other pages' main points.
  2. Add a small section to "operators" page so people know that infix notation doesn't always behave like you'd expect.

knight-of-zero avatar Apr 12 '21 17:04 knight-of-zero

Agreed. Pull requests with these improvements would be welcome.

SethTisue avatar Aug 10 '21 20:08 SethTisue