rum*_*cho 6 design-patterns scala nullable
对于我的第一个Scala程序,我正在尝试编写一个小工具,它将XML文件从一个模式转换为另一个模式.
我开始编写一个方法,它将为我提供文件内容:
  def loadFile(filename: String, encoding: String = "utf-8"):Option[String] = {
    try
    {
      val source = scala.io.Source.fromFile(filename, encoding)
      val contents = source.mkString
      source.close()
      return Some(contents)
    }
    catch 
    {
      return None
    }
  }
但它没有编译.我回来了"值apply不是Nothing的成员"和"值isDefinedAt不是Nothing的成员"作为行的错误消息return None.
我可以找到返回选项使用匹配的所有示例,但这在这里没有意义.如果由于某种原因我无法读取文件,我只想不失败.
在这种情况下我该怎么办?在Scala中有这种做事的模式吗?
在这种特殊情况下(异常处理),我建议使用Try代替.
def loadFile(filename: String, encoding: String = "utf-8"):Option[String] = {
    Try {
      val source = scala.io.Source.fromFile(filename, encoding)
      val contents = source.mkString
      source.close()
      return Some(contents)
    }.toOption
}
但是,我会建议不要删除异常.你通过返回来吞下错误的原因None:是FileNotFoundException吗?一个标准IOException?是否有错误消息(Unsupported encoding想到......)?
我的经验法则是让调用者处理异常.如果他不关心错误本身,处理事情就像:
Try {loadFile("test.txt")}.toOption
更好的是,由于Try具有所有必需的方法,因此可以以相当简洁的方式用于理解:
for(s <- Try {loadFile("test.txt")};
    i <- Try {s.toInt}) yield i
这将导致a Success[Int]或a Failure包含一个描述出错的异常.
关于这一切"catch".
在scala中,它应该像这样使它编译:
  def loadFile(filename: String, encoding: String = "utf-8"):Option[String] = {
    try {
      val source = scala.io.Source.fromFile(filename, encoding)
      val contents = source.mkString
      source.close()
      Some(contents)
    } catch {
      case x: IOException =>  None
      case x => errorHandler(x) // if any other exception
    }
  }
  def errorHandler(e: Any) = None // put some logic here..
所以使用:
catch { case: x:ExceptionType ={ .. handling .. }}
在Scala中catch是一个接受另一个函数作为参数的函数.因此,拥有你所拥有的东西会抱怨应用功能.case提供catch想要的功能(PartialFunction).(简而言之)
注:所有的例外是unchecked在Scala连IOException.
| 归档时间: | 
 | 
| 查看次数: | 6077 次 | 
| 最近记录: |