0
val LIST = scala.collection.mutable.MutableList[String]()
  val filterF = new Function[Path, Boolean] {
    def apply(x: Path): Boolean = {
      println("looking into  " + x)
      val flag = if (x.toString.split("/").last.split("_").last.toLong < System.currentTimeMillis) {
        println("considered " + x)
        LIST += x.toString
        return true
      } else {
        println("NOT considered " + x)
        return false
      }
      return flag
    }
  }

I am trying to update the external variable LIST inside the function filterF. But the problem is that after the println("looking into "+x) line the rest of the code is unreachable.

 val flag = if (x.toString.split("/").last.split("_").last.toLong < System.currentTimeMillis) {
        println("considered " + x)
        LIST += x.toString
        return true
      } else {
        println("NOT considered " + x)
        return false
      }
      return flag

I can't understand why this code is unreachable. Is there some character in the code that is actually reason for this?

Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40
Vijay Krishna
  • 1,037
  • 13
  • 19

2 Answers2

2

This is caused the flag is val not def, but your statement is using the return to return true or false. the return keywords is only for method not for function.

The correct way maybe like:

val flag = if (x.toString.split("/").last.split("_").last.toLong < System.currentTimeMillis) {
  println("considered " + x)
  LIST += x.toString
  true
}
else {
  println("NOT considered " + x)
  false
}
chengpohi
  • 14,064
  • 1
  • 24
  • 42
2

Do not use return

When you use return control of execution will leave the function and all code after the return statement will not be reachable

code after return will be unreachable

def foo: Int = {
 return 1
 2 + 3 //unreachable
}

In case of if expression

def bar: Int = {
  if (true) {
   return 1
  } else {
   return 2
  }
  1 + 2 //unreachable
}

In Scala return statement is optional and not recommended as its not consider functional coding practice.

The value of last expression in the code block is the return value in Scala. So don't worry about explicit return just leave it to the program

 val flag = if (x.toString.split("/").last.split("_").last.toLong < System.currentTimeMillis) {
    println("considered " + x)
    LIST += x.toString
    true //removed return
  } else {
    println("NOT considered " + x)
    false // removed return 
  }

Halting the program executing by throwing an exception or by returning value or by explicitly calling exit is not functional way of doing things. Unfortunately Scala does allow it. But if you want to be a good citizen of functional world. You better avoid it.

Avoid mutable collections

Use mutable collections if you have a strong need for it. There are advantages of immutable collections

1) They are thread safe.

2) Bug free (no surprises by accidental mutations and no blocking).

3) Referential transparency.

4) Reasonable performance.

Use immutable list instead of mutable list.

Use Scala lambda notation and Syntactic sugar

Syntactic sugar is there for a reason. Syntactic sugar reduces the boilerplate code. So that your code looks clear, cleaner and better. Helps in code maintainability. Code remains bug free for longer time.

Instead of Function1 trait use lambda notation.

scala> val f = new Function1[String, String] { def apply(str: String): String =  str}
f: String => String = <function1>

scala> val f = {str: String => str}
f: String => String = <function1>

So your code becomes

val paths = List[Path]() //assume you have Paths in it.

val filter = {path: Path => path.toString.split("/").last.split("_").last.toLong < System.currentTimeMillis }

val resultList = paths.filter(filter)
Nagarjuna Pamu
  • 14,737
  • 3
  • 22
  • 40