gpt4 book ai didi

Scala - 为什么不能推断包含 Vector 和 List 的列表的 Seq 列表?

转载 作者:行者123 更新时间:2023-12-01 10:09:12 24 4
gpt4 key购买 nike

在 2.8.1/2.9.0.1 REPL 中尝试以下操作,第一个给出错误。

val l = List(Vector(1,2), List(3,4,5))
error: type mismatch;
found : scala.collection.immutable.Vector[Int]
required: scala.collection.immutable.Seq[Int]{def companion: scala.collection.generic.GenericCompanion[scala.collection.immutable.Seq[Any]]; protected def thisCollection: Seq[Int]{def companion: scala.collection.generic.GenericCompanion[Seq[Any]]}; def dropRight(n: Int): scala.collection.immutable.Seq[Int]{def companion: scala.collection.generic.GenericCompanion[scala.collection.immutable.Seq[Any]]}; def takeRight(n: Int): scala.collection.immutable.Seq[Int]{def companion: scala.collection.generic.GenericCompanion[scala.collection.immutable.Seq[Any]]}; def slice(start: Int,end: Int): scala.collection.immutable.Seq[Int]{def companion: scala.collection.generic.GenericCompanion[scala.collection.immutable....
val l = List(Vector(1,2), List(3,4,5))
^
:5: error: type mismatch;
found : List[Int]
required: scala.collection.immutable.Seq[Int]{def companion: scala.collection.generic.GenericCompanion[scala.collection.immutable.Seq[Any]]; protected def thisCollection: Seq[Int]{def companion: scala.collection.generic.GenericCompanion[Seq[Any]]}; def dropRight(n: Int): scala.collection.immutable.Seq[Int]{def companion: scala.collection.generic.GenericCompanion[scala.collection.immutable.Seq[Any]]}; def takeRight(n: Int): scala.collection.immutable.Seq[Int]{def companion: scala.collection.generic.GenericCompanion[scala.collection.immutable.Seq[Any]]}; def slice(start: Int,end: Int): scala.collection.immutable.Seq[Int]{def companion: scala.collection.generic.GenericCompanion[scala.collection.immutable.Seq[Any]]}; def take(n: Int):...
val l = List(Vector(1,2), List(3,4,5))
^

虽然这成功了:

val l = List[Seq[Int]](Vector(1,2), List(3,4,5))
//evaluates fine to List[Seq[Int]] = List(Vector(1, 2), List(3, 4, 5))

scala 在第一种情况下尝试推断的类型是什么?它是具有结构类型的 Seq 吗?为什么不能统一Vector和List呢?这是一些缺失的功能吗,还是这样(需要明确的类型定义)以防止搬起石头砸自己的脚?

最佳答案

这显然是类型推断器中的错误,现在已在 Scala 2.9.1修复:

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) Server VM, Java 1.6.0_18).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import collection.immutable._
import collection.immutable._

scala> List(Vector(1, 2, 3), List(4, 5))
res0: List[scala.collection.immutable.Seq[Int]] = List(Vector(1, 2, 3), List(4, 5))

关于Scala - 为什么不能推断包含 Vector 和 List 的列表的 Seq 列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7411809/

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