Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm used to delimit statements by a semicolon from Java, so naturally I do it in Scala code too. I also feel that the code is easier to read, because it's evident where one statement ends and another begins. But many times when I post a piece of Scala code on SO, the code gets edited just to get semicolons removed.

  1. Should I use semicolons or not? Are there any "official" guidelines or coding style?
  2. Are there cases where semicolons are required, otherwise the code is ambiguous?
share|improve this question
Has Haskell taught you nothing? ;P remove unnecessary syntax, semicolons should only be used to break up multiple clauses/statements on the same line – Jimmy Hoffa Jul 10 '13 at 12:58
@JimmyHoffa Haskell taught me that syntax must be beautiful and consistent :). – Petr Pudlák Jul 10 '13 at 13:02
The first part of that is why I get annoyed whenever I see Scala snippets... – Jimmy Hoffa Jul 10 '13 at 14:28

How much do you want to understand the languages you use? Do you want to play to its strengths or not? Scala's syntax encourages clear and concise code; the few edge cases where taking advantage of this causes problems for the Scala parser are instructive (that is, you learn more about Scala by encountering and learning how to avoid them) and gradually being fixed (e.g. suffix notation). Defensively using the semi-colon may mean you won't ever have to learn about these issues but do you really see that as a good thing? The fixes for these issues usually have other implications but you will miss out on these possibilities.

Another consideration is that, as Kilian concedes, most Scala developers omit semi-colons by default. How do you expect to work with other people's code if you avoid using idiomatic Scala yourself? You're going to find it much harder than it need be.

I can't emphasis enough that these features of the Scala parser were chosen to encourage clean, re-usable and functional (in the fp sense) code. Take infix operator syntax as an example; it encourages developers to furnish classes with simple, single-purpose methods which compose well together. Scala's collection library shows just how beatifully that can work. Classically-trained Java devs who embrace this will tend to develop better habits and new ways of thinking about their code. Those who stick to all those familiar dots and brackets will miss out. I contend that this is also true for those too timid to abandon the semi-colon.

share|improve this answer
-1 yes, being "forced" to learn the language due to compiler gotchas went out of style with C++. Also, there's little evidence behind the assertions, which I would want for an answer with such biased tone. – Telastyn Aug 27 '13 at 16:42

There is no officially "right" way of doing it, but most practitioners omit semicolons wherever they can (which is why so many people remove semicolons reflexively).

The language tries very hard to make you forget about the need to terminate statements at all. Unfortunately, it doesn't quite succeed, so there are various intricate situations in which semicolons, or an extra set of parenthesis, or similar "hints" are necessary to avoid unwanted semantics. Such situations are almost rare enough that normally you don't have to think about them. Opinions differ on whether this means you should punctuate defensively (that's what I think) or never punctuate until you run into trouble (that's what the majority of users think). Ultimately, it comes own to whether you self-identify as a renegade or a model citizen.

share|improve this answer
Thanks, could you perhaps give an example (or a few :)) of such intricate situations? – Petr Pudlák Jul 10 '13 at 10:17
I was afraid I'd get asked this... Programming in Scala lists several of them that surprised me, but I don't have the book with me at the moment, and I forget the details. That's the point I was trying to make: if I could remember these exceptional situations it wouldn't be so bad, but as it is, I always feel uncertain whether I can really omit the semicolon - so I don't. – Kilian Foth Jul 10 '13 at 10:47
Huh. I've never once run into a situation where missing a semicolon in Scala has hurt me. I never include them. I've also only programmed Scala for hobby-like things though, so maybe it will bite me sooner or later. – KChaloux Jul 10 '13 at 14:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.