3

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ですか?ベクターとリストを統合できないのはなぜですか?これはまだいくつかの不足している機能ですか、それとも自分の足を撃たないようにするためにこの方法(明示的なタイプdefが必要)ですか?

4

2 に答える 2

7

これは明らかに型推論のバグであり、scala2.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))
于 2011-09-14T07:05:57.387 に答える
0

ここでのダニエルの答えに基づいて、ScalaはHindley-Milner型推論を使用せず、代わりにローカル型推論を実行し、左から右に移動します。

最初の宣言では、最初のリストメンバーはVector[Int]、Scalaが「わかりましたList[Vector[Int]]が、2番目のリスト要素aList[Int]に到達すると、これをと統合できなくなりVector[Int]ます。ジェネリックは推論者にとって問題になるはずです。数字と文字列を含むリストは、適切に推測できList[Any]ます。

関連するもの:ベクトルとリストは、演算子全体で2.9.0.1で相互運用できます==

scala> List[Int](1,2,3) == Vector[Int](1,2,3)
res2: Boolean = true

scala> List[Int](1,2,3) == Vector[Int](1,12,3)
res3: Boolean = false
于 2011-09-14T06:07:51.460 に答える