実際、デフォルトで Some[X] から GenTraversableOnce[X] への暗黙の変換があります。これは、REPL でテストするのが非常に簡単です。
scala> implicitly[Function[Some[Int],GenTraversableOnce[Int]]]
res1: Some[Int] => scala.collection.GenTraversableOnce[Int] = <function1>
scala> implicitly[Some[Int] => GenTraversableOnce[Int]] // alternative syntax
res2: Some[Int] => scala.collection.GenTraversableOnce[Int] = <function1>
実際、これはオブジェクト Option で定義されています。scala パッケージ内:
object Option {
/** An implicit conversion that converts an option to an iterable value
*/
implicit def option2Iterable[A](xo: Option[A]): Iterable[A] = xo.toList
/** An Option factory which creates Some(x) if the argument is not null,
* and None if it is null.
*
* @param x the value
* @return Some(value) if value != null, None if value == null
*/
def apply[A](x: A): Option[A] = if (x == null) None else Some(x)
/** An Option factory which returns `None` in a manner consistent with
* the collections hierarchy.
*/
def empty[A] : Option[A] = None
}
option2Iterable はまさにあなたが探しているものです。また、REPL でテストすると、GenTraversableOnce の実装がリストであることがわかる理由もわかります。
何もせずに自動的にインポートされる暗黙的な変換 (暗黙的に使用する REPL で確認できるものなど) を探している場合は、以下を確認する必要があります。
- Predef.scala
- クラスのコンパニオン オブジェクト