किसी कारण से, shuffle
के लिए प्रकार निष्कर्ष के रूप में एक Range
करने का विरोध एक Inclusive
के लिए एक अलग परिणाम पैदा करता है।
override def toSeq = this
वहाँ के बजाय ओवरराइड विधि का परिणाम प्रकार अनुमान लगाने के लिए एक खुला मुद्दा है:
कारण toSeq
परिणाम Range
में है कि इसकी परिभाषा मासूम प्रकार सीमित कर देता है है।
दिखा कि आरईपीएल झूठ नहीं बोल रही है:
scala> import util.Random.shuffle
import util.Random.shuffle
scala> val x = 1 to 10
x: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> val y = x.toSeq
y: scala.collection.immutable.Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> val z: Range = x
z: Range = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> shuffle(x)
res0: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 5, 2, 10, 9, 6, 3, 7, 4, 8)
scala> shuffle(y)
<console>:11: error: Cannot construct a collection of type scala.collection.AbstractSeq[Int] with elements of type Int based on a collection of type scala.collection.AbstractSeq[Int].
shuffle(y)
^
देखने के लिए क्या निष्कर्ष निकाला गया था और क्या निहित इस्तेमाल किया गया था Snipping:
scala> :replay -Xprint:typer
Replaying: shuffle(x)
private[this] val res0: scala.collection.immutable.IndexedSeq[Int] = scala.util.Random.shuffle[Int, scala.collection.immutable.IndexedSeq]($line5.$read.$iw.$iw.x)(immutable.this.IndexedSeq.canBuildFrom[Int]);
scala> shuffle(y)
private[this] val <res1: error>: <error> = scala.util.Random.shuffle[Int, scala.collection.AbstractSeq]($line6.$read.$iw.$iw.y)();
बजाय आप क्या आशा व्यक्त की:
scala> shuffle[Int, collection.immutable.IndexedSeq](z)
res3: scala.collection.immutable.IndexedSeq[Int] = Vector(6, 5, 3, 8, 4, 1, 2, 10, 7, 9)
-Ytyper-debug
के साथ, एक अतिरिक्त प्रकार का param A
है जो इसे प्रक्षेपित करता है Inclusive
के लिए एड, लेकिन मुझे पता नहीं है कि यह कहां से आता है।
| | | | |-- x BYVALmode-EXPRmode-POLYmode (site: value res3 in $iw)
| | | | | \-> scala.collection.immutable.Range.Inclusive
| | | | solving for (T: ?T, CC: ?CC)
| | | | solving for (A: ?A)
| | | | [adapt] [A]=> scala.collection.generic.CanBuildFrom[scala.collect... adapted to [A]=> scala.collection.generic.CanBuildFrom[scala.collect... based on pt scala.collection.generic.CanBuildFrom[scala.collection.immutable.IndexedSeq[Int],Int,scala.collection.immutable.IndexedSeq[Int]]
| | | | |-- [T, CC[X] <: TraversableOnce[X]](xs: CC[T])(implicit bf: ... EXPRmode (site: value res3 in $iw)
| | | | | \-> scala.collection.immutable.IndexedSeq[Int]
| | | | [adapt] [T, CC[X] <: TraversableOnce[X]](xs: CC[T])(implicit bf: ... adapted to [T, CC[X] <: TraversableOnce[X]](xs: CC[T])(implicit bf: ...
| | | | \-> scala.collection.immutable.IndexedSeq[Int]
क्या यह एक बग या व्यवहार है?
यह स्पष्ट करना:
scala> import language.higherKinds, collection.TraversableOnce, collection.generic.CanBuildFrom
import language.higherKinds
import collection.TraversableOnce
import collection.generic.CanBuildFrom
scala> def f[T, CC[X] <: TraversableOnce[X]](xs: CC[T])(implicit cbf: CanBuildFrom[CC[T],T,CC[T]]): CC[T] = null.asInstanceOf[CC[T]]
f: [T, CC[X] <: scala.collection.TraversableOnce[X]](xs: CC[T])(implicit cbf: scala.collection.generic.CanBuildFrom[CC[T],T,CC[T]])CC[T]
scala> f(1 to 10)
res0: scala.collection.immutable.IndexedSeq[Int] = null
scala> f(1 until 10)
<console>:12: error: Cannot construct a collection of type scala.collection.AbstractSeq[Int] with elements of type Int based on a collection of type scala.collection.AbstractSeq[Int].
f(1 until 10)
^
https://gitter.im/scala/scala?at=554d5ab5675c1d50549ec0f1 तर्कसंगत रूप से अधिक अभिव्यक्तिपूर्ण है। –