Scala:Option、Some和ArrowAssoc操作符

Scala:Option、Some和ArrowAssoc操作符,scala,Scala,我试图分析以下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],

我试图分析以下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])]
Option[(DirectoryStream[Path], Iterator[Path])]
据我所知,->操作符是scala.Predef包中的ArrowAssoc

implicit final class ArrowAssoc[A] extends AnyVal
但我仍然不明白->是如何给我返回类型为的值的:

Option[(DirectoryStream[Path], Iterator[Path])]
Option[(DirectoryStream[Path], Iterator[Path])]

Scala的专家们能对这一点有更多的了解吗?有没有办法用一种更易读的方式来写“一些(…)”的东西?不过,我确实理解一些人所扮演的角色。

操作符只创建了一个元组:

scala> 1 -> "one"
res0: (Int, String) = (1,one)
这相当于

scala> (1, "one")
res1: (Int, String) = (1,one)
(1, "foo")
我只是想添加源代码,但Reactormonk已经先到了;-)


->
方法可通过隐式ArrowAssoc类在任何对象上使用。在
A
类型的对象上调用它,传递
B
类型的参数,创建
Tuple2[A,B]
运算符的常见情况是

Map(1 -> "foo", 2 -> "bar")
这和

Map((1, "foo"), (2, "bar"))
因为
Map.apply
的签名是

def apply[A, B](elems: Tuple2[A, B]*): Map[A, B]
这意味着它将元组作为参数,并从 它

所以

相当于

scala> (1, "one")
res1: (Int, String) = (1,one)
(1, "foo")
从编译器源代码:

implicit final class ArrowAssoc[A](private val self: A) extends AnyVal {
  @inline def -> [B](y: B): Tuple2[A, B] = Tuple2(self, y)
  def →[B](y: B): Tuple2[A, B] = ->(y)
}

它直接告诉你它正在创建一个元组。那
1→ “foo”
同样有效。

我将接受您的答案,因为您确实说过退货类型。How@ReactorMonk的回答也很好。非常感谢。两个答案在目标上都同样正确。我明白我有点困惑@Dan说,->操作符创建一个元组。在您给出的地图示例1->“食物”中,键值对是一个元组,通过它可以创建地图?非常感谢。你的答案很详细,而且准确无误。在寻求关于元组的澄清之后,您添加了更多的上下文,并在@Dan回答后巩固了我的理解。我认为除了用逗号代替
->
之外,没有更好的表达方式了。到底为什么要导入scala.Some?过分热心的IDE?