无法构造类型的集合...包含[Long]类型为Long的元素基于类型的集合...包含[Long]

Era*_*dan 3 scala typechecking scala-collections

我不确定我理解为什么会发生以下情况.

编译和工作:

随着Int•不用转换成List

import scala.util.Random
val xs = 1 to 10
Random.shuffle(xs)
Run Code Online (Sandbox Code Playgroud)

Longs转换成一个List

import scala.util.Random
val xs = 1L to 10L
Random.shuffle(xs.toList) //<-- I had to materialize it to a list
Run Code Online (Sandbox Code Playgroud)

不编译

随着Long小号没有转换成List

val xs = 1L to 10L
Random.shuffle(xs) 
Run Code Online (Sandbox Code Playgroud)

这个引发了这个异常:

Error: Cannot construct a collection of type 
scala.collection.immutable.NumericRange.Inclusive[Long] with elements of type 
Long based on a collection of type 
scala.collection.immutable.NumericRange.Inclusive[Long].
  Random.shuffle(xs)
                ^

我很好奇为什么?是因为缺少CanBuildFrom或类似的东西吗?有没有一个很好的理由没有一个?

(scala版本2.11.5)

dk1*_*k14 6

这是因为CanBuildFrom(1)和类型推断机制(2).

1)您可能会发现genericBuilderRange/ NumericRange(同为Inclusive)是:

 genericBuilder[B]: Builder[B, IndexedSeq[B]]
Run Code Online (Sandbox Code Playgroud)

所以只有CanBuildFrom[Range, B, IndexedSeq],它使用这个构建器.原因很简单,您可以在构建器的描述中找到它:

通过使用+ =向构建器添加元素,然后使用result转换为所需的集合类型,构建器允许逐个构建集合.

你不能逐步构建包含范围,因为它不再是一个范围(但仍然是一个IndexedSeq); 但是,你可以用这样的结构Seq.

只是为了演示的区别IndexedSeqInclusive

scala> (1 to 5)
res14: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5)

scala> (1 to 5) ++ (7 to 10) //builder used here
res15: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 7, 8, 9, 10)
Run Code Online (Sandbox Code Playgroud)

这意味着您不能"构建"任何范围,无论是Int(Range)还是Long(Numeric),您应该始终IndexedSeq作为To构建器的参数传递.但是,当您将其传递给函数时,IndexedSeq会自动为Int(Range)指定shuffle.

2)它不起作用,NumericRange.Inclusive[T]因为它是一个多态类型(通用).然而,常规Range.Inclusive(非通用)明确扩展IndexedSeq[Int].看着随机签名:

shuffle[T, CC[X] <: TraversableOnce[X]](xs: CC[T])(implicit bf: CanBuildFrom[CC[T], T, CC[T]]): CC[T]
Run Code Online (Sandbox Code Playgroud)

高阶类型CC正在变为NumericRange.Inclusive这里,因为它是继承的最大参数化类型NumericRange.Inclusive.在这种情况下Range.Inclusive,那是一个IndexedSeq(因为较小Range.Inclusive的不是通用的).所以Range.Inclusive很幸运不受(1)的影响.

最后,这将工作:

scala> Random.shuffle[Long, IndexedSeq](xs)
res8: IndexedSeq[Long] = Vector(9, 3, 8, 6, 7, 2, 5, 4, 10, 1)

scala> Random.shuffle(xs: IndexedSeq[Long])
res11: IndexedSeq[Long] = Vector(6, 9, 7, 3, 1, 8, 5, 10, 4, 2)
Run Code Online (Sandbox Code Playgroud)