"?:" operator is scala

154 views Asked by At

In Java, I can write:

Double x = (x1 > x2)? x1:x2

But this doesn't seem to work in Scala, the following has an error:

var x = (x1 > x2)? x1:x2

I don't feel like writing a block of code for this in Scala:

var x = x2

if (x1 > x2 ) {
    x = x1
}

If there a cleaner way for such operation in Scala? Thanks!

1

There are 1 answers

0
Raniz On BEST ANSWER

According to this and this page you just use the regular if/else syntax:

var x = if(x1 > x2) x1 else x2