gpt4 book ai didi

scala - 为什么这个 flatMap 会失败?

转载 作者:行者123 更新时间:2023-12-04 11:40:17 25 4
gpt4 key购买 nike

scala> val s: Seq[Class[_ <: java.lang.Enum[_]]] = Seq(classOf[java.util.concurrent.TimeUnit])
s: Seq[Class[_ <: java.lang.Enum[_]]] = List(class java.util.concurrent.TimeUnit)

scala> s.flatMap(_.getEnumConstants)
<console>:9: error: no type parameters for method flatMap: (f: Class[_ <: java.lang.Enum[_]] => scala.collection.GenTraversableOnce[B])(implicit bf: scala.collection.generic.CanBuildFrom[Seq[Class[_ <: java.lang.Enum[_]]],B,That])That exist so that it can be applied to arguments (Class[_ <: java.lang.Enum[_]] => scala.collection.mutable.ArrayOps[_$1(in value $anonfun) with java.lang.Object] forSome { type _$1(in value $anonfun) <: java.lang.Enum[_] })
--- because ---
argument expression's type is not compatible with formal parameter type;
found : Class[_ <: java.lang.Enum[_]] => scala.collection.mutable.ArrayOps[_$1(in value $anonfun) with java.lang.Object] forSome { type _$1(in value $anonfun) <: java.lang.Enum[_] }
required: Class[_ <: java.lang.Enum[_]] => scala.collection.GenTr... s.flatMap(_.getEnumConstants)

最佳答案

不是真正的准确答案,而是两个观察结果 - Scala 2.10 会给你一个更好的错误:

scala> s.flatMap(_.getEnumConstants)
<console>:9: error: no type parameters for method flatMap: (f: Class[_ <: Enum[_]] => scala.collection.GenTraversableOnce[B])(implicit bf: scala.collection.generic.CanBuildFrom[Seq[Class[_ <: Enum[_]]],B,That])That exist so that it can be applied to arguments (Class[_ <: Enum[_]] => scala.collection.mutable.ArrayOps[(some other)_$1(in object $iw) with Object] forSome { type (some other)_$1(in object $iw) <: Enum[_] })
--- because ---
argument expression's type is not compatible with formal parameter type;
found : Class[_ <: Enum[_]] => scala.collection.mutable.ArrayOps[(some other)_$1(in object $iw) with Object] forSome { type (some other)_$1(in object $iw) <: Enum[_] }
required: Class[_ <: Enum[_]] => scala.collection.GenTraversableOnce[?B]
s.flatMap(_.getEnumConstants)
^
<console>:9: error: type mismatch;
found : Class[_ <: Enum[_]] => scala.collection.mutable.ArrayOps[(some other)_$1(in object $iw) with Object] forSome { type (some other)_$1(in object $iw) <: Enum[_] }
required: Class[_ <: Enum[_]] => scala.collection.GenTraversableOnce[B]
s.flatMap(_.getEnumConstants)
^
<console>:9: error: Cannot construct a collection of type That with elements of type B based on a collection of type Seq[Class[_ <: Enum[_]]].
s.flatMap(_.getEnumConstants)
^

而且,如果您拆分 flatMap ,你会看到这个问题的一个更简单的版本:
scala> s.map(_.getEnumConstants)
res28: Seq[Array[_$1 with Object] forSome { type _$1 <: Enum[_] }] = List(Array(NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS), Array(RELEASE_0, RELEASE_1, RELEASE_2, RELEASE_3, RELEASE_4, RELEASE_5, RELEASE_6))
scala> res28.flatten
<console>:10: error: No implicit view available from Array[_$1 with Object] forSome { type _$1 <: Enum[_] } => scala.collection.GenTraversableOnce[B].
res28.flatten
^

这是相当令人惊讶的,因为您认为转动 Array 应该很容易。成 GenTraversableOnce .我目前没有时间挖掘细节,但我会指出以下事情似乎有效:
s.flatMap(_.getEnumConstants.toSeq)
s.flatMap(_.getEnumConstants.map(_.asInstanceOf[Enum[_]]))

我投票编译器错误,因为 this gist ,它显示了这个简单脚本在 REPL 中的一些非常奇怪的行为
val s: Seq[Class[_ <: java.lang.Enum[_]]] = Seq(classOf[java.util.concurrent.TimeUnit], classOf[javax.lang.model.SourceVersion])
s.flatMap(_.getEnumConstants.toSeq)
s.flatMap(_.getEnumConstants.toArray)
1234

关于scala - 为什么这个 flatMap 会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15354771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com