0

I am very new to Scala and don't know how to swap out vars and use vals into the code. There are lots of vars in the code.

Below is just a piece of code:

var fa = new JsArray

val ra = Json.obj("key1" -> val1, "key2" -> val2, "key3" -> val3)

fa = fa.:+(ra)

How to use val for fa ?

2nd Case:

val a = 10
var dr: String = ""
if (a == 10) {
    dr = "true"
}
else {
    dr = "false"
}
println("dr: " + dr)

Here, if I put val for "dr" inside if/else then it cannot be access in println(). Is there anything help on it to avoid var ?

Buhb
  • 7,088
  • 3
  • 24
  • 38
iNikkz
  • 3,729
  • 5
  • 29
  • 59

5 Answers5

4

In Scala, if/else is an expression that returns a value.

val dr = if (condition) value1 else value2
jwvh
  • 50,871
  • 7
  • 38
  • 64
2

Pattern matching is a good option to make dr a val, especially if you need to test a for multiple different values.

val a = 10

val dr = a match {
  case 10 => true
  case _ => false
}

print(dr)

case 10 => true This essentially says "if a matches 10, then assign true to dr"
case _ = false This says "if none of the above case(s) matched a then assign false to dr"

Note that dr will be a Boolean type in the above sample, which is much better than using a String to represent true/false. If you really need a string for some reason then you would do.

val dr = a match {
  case 10 => "true"
  case _ => "false"
}

print(dr)
jacks
  • 4,614
  • 24
  • 34
  • Is there anything that replace "var" ? I think, "var" is not good to use in Scala. – iNikkz Dec 06 '16 at 09:10
  • There are no `var`s in above example now. In general, there are some use cases for mutable variables but you are right to stay away from them wherever you can. See that `dr` is a `val` not a `var` in the pattern match. – jacks Dec 06 '16 at 09:13
  • Yup. I got. I need to understand more in mutable variables? Is there anything help stuffs ? – iNikkz Dec 06 '16 at 09:16
  • Just google scala val vs var and you will find 1000's of articles on the subject. Start here maybe for a quick read - http://stackoverflow.com/questions/1791408/what-is-the-difference-between-a-var-and-val-definition-in-scala?answertab=active#tab-top – jacks Dec 06 '16 at 09:22
  • I think using pattern matching in that case is an overkill and simple if expression would be more appriopriate. – adamwy Dec 06 '16 at 12:51
  • 3
    Or just write: `val dr = a == 10`. – adamwy Dec 06 '16 at 13:01
1

If you use a val, you're not allowed to assign it a new value, so in your code example above, if you switch the third line with

val fa2 = fa.:+(ra)

then fa can be a val.

Buhb
  • 7,088
  • 3
  • 24
  • 38
1

The pattern matching is probably the best style for this case, but if you wanted the if else you could write is as this. Note in Scala when the if statement is evaluated it returns a result like any other code block.

val a = 10
val dr = if (a == 10) {
   "true"
}
else {
    "false"
}
println("dr: " + dr)
Gary
  • 46
  • 2
1

I'll just add, that perhaps instead of representing dr as a String it would be better to type it as a Boolean, and then you can simply evaluate it to the result of the condition:

val a = 10
val dr = a == 10
println("dr: " + dr)
adamwy
  • 1,239
  • 1
  • 7
  • 12