【问题标题】:unreachable code in scalascala中无法访问的代码
【发布时间】:2016-11-04 01:01:29
【问题描述】:
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
    }
  }

我正在尝试更新函数filterF 内的外部变量LIST。但问题是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

我不明白为什么这段代码无法访问。代码中是否有某些字符实际上是造成这种情况的原因?

【问题讨论】:

  • 您确实需要更好地格式化您的代码。那些在线运行的东西读起来很痛苦。
  • 幽默我;你试过 'System.out.println("looking into "+x)' 吗?
  • 请取出脚本中的每个分号并重新粘贴。一个语句 = 一行
  • 进行了编辑。希望它看起来更好。很抱歉给您带来不便。认为这是新手的错误。
  • @VijayKrishna 格式化了您问题中的代码并回答了它。请检查

标签: scala function unreachable-code


【解决方案1】:

不要使用返回

当你使用return时,执行控制将离开函数,并且return语句之后的所有代码都将无法访问

返回后的代码将无法访问

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

如果是if表达式

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

在 Scala 中,return 语句是可选的,不推荐使用,因为它不考虑函数式编码实践。

代码块中最后一个表达式的值是Scala中的返回值。所以不用担心显式返回,把它留给程序吧

 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 
  }

通过抛出异常、返回值或显式调用 exit 来停止程序的执行并不是功能性的做事方式。不幸的是,Scala 确实允许这样做。但是,如果您想成为功能世界的好公民。你最好避免它。

避免可变集合

如果您有强烈的需求,请使用 mutable 集合。 immutable收藏有优势

1) 它们是线程安全的。

2) 无错误(不会因意外突变和阻塞而感到意外)。

3) 参照透明。

4) 性能合理。

使用immutable 列表而不是mutable 列表。

使用 Scala lambda 表示法和语法糖

语法糖的存在是有原因的。语法糖减少了样板代码。让你的代码看起来更清晰、更干净、更好。有助于代码的可维护性。代码在较长时间内保持无错误。

使用 lambda 表示法代替 Function1 特征。

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>

所以你的代码变成了

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)

【讨论】:

    【解决方案2】:

    这是因为 flagval 而不是 def,但您的语句使用 return 返回 true 或 假。 return 关键字仅用于方法而不是功能。

    正确的方法可能是:

    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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多