1

I'm new in the scala programming, so i'm seeing some basic programs.

I noticed that the instructions end with ; character in some case, while in other case they end without any character.

What is the difference?

Gaël J
  • 11,274
  • 4
  • 17
  • 32
  • 1
    https://stackoverflow.com/questions/29743009/in-scala-are-semicolons-necessary-in-some-situations – luk2302 Oct 21 '22 at 16:04
  • 2
    Probably they are just used to put `;` from other languages. In **Scala** you almost never need `;` - only when you inline a ballock which is a very weird thing to do. – Luis Miguel Mejía Suárez Oct 21 '22 at 16:04

1 Answers1

4

None, really. Scala allows you to explicitly insert semicolons but it's not mandatory and they are inserted for you.

From my experience, the overwhelming majority of Scala developers tend to prefer not using semicolons explicitly.

The only case in which I've seen semicolons used is when you have multiple expressions in a for-comprehension and you don't want to necessarily break those apart over multiple lines, in which case you would turn something like this:

for {
  a <- 1 to 10
  b <- 1 to 10
} yield a * b

into

for (a <- 1 to 10; b <- 1 to 10) yield a * b

But then again, some prefer to always break those kind of expressions over multiple lines anyway for consistency, which is why scalafmt (a Scala code formatter) has a rule precisely for that.

stefanobaghino
  • 11,253
  • 4
  • 35
  • 63