【发布时间】:2015-11-22 02:34:48
【问题描述】:
我正在尝试分析以下 Scala 代码:
import java.nio.file._
import scala.Some
abstract class MyCustomDirectoryIterator[T](path:Path,someNumber:Int, anotherNum:Int) extends Iterator[T] {
def getCustomIterator(myPath:Path):Option[(DirectoryStream[Path],
Iterator[Path])] = try {
//we get the directory stream
val str = Files.newDirectoryStream(myPath)
//then we get the iterator out of the stream
val iter = str.iterator()
Some((str -> iter))
} catch {
case de:DirectoryIteratorException =>
printstacktrace(de.getMessage)
None
}
如何解释这段代码:Some((str -> iter))
是的,它返回一个类型的值:
Option[(DirectoryStream[Path], Iterator[Path])]
据我所知,-> 运算符是 scala.Predef 包中的 ArrowAssoc。
implicit final class ArrowAssoc[A] extends AnyVal
但我仍然不明白 -> 的作用是什么给我一个类型的返回值:
Option[(DirectoryStream[Path], Iterator[Path])]
这里的 Scala 专家能否进一步阐明这一点?有没有办法以更易读的方式编写“Some(..)”?不过,我确实理解 Some 所扮演的角色。
【问题讨论】:
-
我认为除了使用逗号代替
->之外,没有更好的表达方式。为什么要导入scala.Some?过度热心的 IDE?
标签: scala