Scala: declaring val within for loop, if condition

I'm a scala beginner and trying to understand how val works in Scala. I read that vals cannot be modified. When I do the following:

for( line <- Source.fromFile(args(0)).getLines() ) {
   val currentLine = line
   println(currentLine)
}

currentLine is updated in each iteration, while I expect it to be initialized with the first line and hold it till the end, or at least give a re-initialization error of some sort. Why is this so? Is the val created and destroyed in each iteration? My second question: I would like to use x outside if in the following code.

if( some condition is satisfied) val x = 2 else val x = 3

As of now, I'm getting an "Illegal start of simple expression" error. Is there a way to use x outside if?

Answers


  1. Yes, the val is created and destroyed on each iteration.

  2. val x = if(condition) 2 else 3 would do what you want.

Edit: You could rewrite 2. to if(conditon) {val x = 2} else {val x = 3} (to make it compile) but that would do nothing, since the if does not return anything and the variable can not be used outside the if


For Loop

You can break it down into a map operation.

for( line <- Source.fromFile(args(0)).getLines() ) {
   val currentLine = line
   println(currentLine)
}

So this code transforms to

Source.fromFile(args(0)).getLines().map( line => block )

block could be any expression. Where in your case block is:

{
   val currentLine = line
   println(currentLine)
}

Here currentLine is local to block and is created for each of the values of line given to map operation.

If-Else

Again following is also wrong:

if( some condition is satisfied) val x = 2 else val x = 3

Essentially if-else in Scala returns a value. So it should be:

if( condition ) expression1 else expression1

In your case you it can be:

if( condition ) { val x = 2 } else { val x = 3 }

However an assignment returns Unit ( or void if you want an analogy with Java / C++ ). So You can simply take the value of if-else like so:

val x = if( condition ) { 2 } else { 3 }
// OR
val x = if( condition ) 2 else 3

No answer mentioned it so in addition to what was said :

  1. The val is made available for garbage collection on each iteration (and thus is not accessible from the next loop iteration). This is due to what is called scope of variables which is limited to the block in scala (same as Java).

  2. As stated by @Kigyo val x = if(condition) 2 else 3 would do what you want, because you do only one assignation. If you put the assignation to val into the blocks, then the scope of this val is the block and thus not usable like you want to.


1st question: yes, in every iteration a new val is created

2nd question: you could rewrite it is

 val x =  if (some condition is satisfied)
  2
 else
  3

Need Your Help

Does phonegap protect my urls?

jquery security phonegap-build

If I have a simple javascript string in my code:

Where is Bea Weblogic Work Folder

oracle jsp weblogic weblogic11g temporary-directory

I know that this may be a trivial question but I really cannot find the work folder in Oracle BEA weblogic 10.3. Unlike the Apache Tomcat where it is clearly in the work directory usually under WEB...