7

I'm studying Scala and trying to use it in my recent projects. And problems come. Here's one of my problem about necessity of semicolons. This is my example:

var str = "123.4"
var d = str toDouble
if(d > 10)
    println("Larger than 10")

These codes cannot be compiled. Because if(d > 10) println("Larger than 10") returns value and compiler thinks this value is a parameter of toDouble method. However, toDouble doesn't have a parameter. This causes error.

The easiest way to solve this is adding a semicolon at the end of line 2. Just like this:

var str = "123.4"
var d = str toDouble;
if(d > 10)
    println("Larger than 10")

This confused me and I thought I don't need semicolons at all as I won't put two statements at same line. It makes me uncomfortable that some lines end with semicolon while the others don't. Also, does it makes sense?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Chaofan
  • 93
  • 1
  • 4
  • 2
    The answer for the question should probably give you some clarity http://stackoverflow.com/questions/10656799/when-are-scala-semicolons-required – Saket Apr 20 '15 at 08:47
  • From my view, the grammar design is wield, it should not be so confusing, and error-prone. – Eric Mar 22 '17 at 06:48
  • Does this answer your question? [When are Scala Semicolons required](https://stackoverflow.com/questions/10656799/when-are-scala-semicolons-required) – daniu Oct 21 '22 at 16:15

4 Answers4

14

Aggregating possible answers: To write same thing without syntax error you could use:

  • Semicolon

    var d = str toDouble;
    if (d > 10) println("Larger than 10")
    
  • Dot Syntax

    var d = str.toDouble
    if (d > 10)  println("Larger than 10")
    
  • Parenthesis

    var d = (str toDouble)
    if (d > 10)  println("Larger than 10")
    
  • Braces

    var d = {str toDouble}
    if (d > 10)  println("Larger than 10")
    
  • Empty line separator

    var d = str toDouble
    
    if (d > 10)  println("Larger than 10")
    

Choose the one which suits your style. But in normal (non-DSL) code you will usually meet dotted version

A [most] usual use of semicolon is inside simple for expressions with several bindings.

  for(i <- 1 to 4; j <- 1 until i) println(f"$j < $i")

Which by the way could be refactored to semicolonless version too:

  for{i <- 1 to 4
      j <- 1 until i} println(f"$j < $i")
Odomontois
  • 15,918
  • 2
  • 36
  • 71
  • 1
    You could also ascribe the type at the end of the line to end the expression: `var d = str toDouble : Double`. – Ben Reich Apr 20 '15 at 13:36
6

Semicolons are sometimes required when using postfix operators. This is part of the reason why postfix operators are a language feature that you'll be warned about if you haven't explicitly enabled them. You could use the str.toDouble syntax instead.

lmm
  • 17,386
  • 3
  • 26
  • 37
2

As mentioned by Imm, postfix operators are an added feature. By importing scala.language.postfixOps you enable the compiler to acccept postfix syntax, hence this works

import scala.language.postfixOps

var str = "123.4"
var d = str toDouble
if(d > 10)
    println("Larger than 10")
elm
  • 20,117
  • 14
  • 67
  • 113
0

Yes, you need to put a semicolon sometimes, to make compiler understand that value next is not a parameter to the previous one. Like in your case:

var str = "123.4"

In code above, string ("123.4") does not takes any paramater so compiler automatically understands that it's end of line and any variable or function next to it is start of new line. But in case:

var d = str toDouble

As we can pass parameters to toDouble method, so compiler will understand that variable next to it is parameter for the same, to avoid these kind of situations we have to make put semicolon and make compiler understand about end of line.

Arpit Aggarwal
  • 27,626
  • 16
  • 90
  • 108