Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "if" as an expression

Tags:

scala

I wonder, why can't I compile this one:

class MyClass{
  override def toString = "123:" + if (true) "456" else "789"
  //error:  illegal start of simple expression
}
like image 991
Alan Coromano Avatar asked Dec 26 '22 01:12

Alan Coromano


1 Answers

Try this:

override def toString = "123:" + (if (true) "456" else "789")
like image 106
pedrofurla Avatar answered Jan 15 '23 16:01

pedrofurla