untilで作ったRangeはshuffleできない
今日のScala悲しみポイント
「untilで作ったRangeはshuffleできない」
↓ なんと、toで作ったRangeとuntilで作ったRangeでは型が違うらしい。
↓ Listのように明示的に列挙されているコレクションに変換すればもちろんshuffleできる。
少し考えてみたけど、なんでこうなっているのか思想がよくわらない。なんか合理的な説明ができるのだろうか。
このくらい暗黙的型変換で解決してほしいなあと思っていたら、実際にはやろうとしていて、型変換時の推論に失敗している模様。明示的にIndexedSeqを指定すると動く。
2015.07.24 追記
indices もシャッフル出来ない。
scala> (0 to 5) res0: scala.collection.immutable.Range.Inclusive = Range(0, 1, 2, 3, 4, 5) scala> Random.shuffle(0 to 5) res1: scala.collection.immutable.IndexedSeq[Int] = Vector(4, 3, 2, 1, 5, 0) scala> val indices = (0 to 5).indices indices: scala.collection.immutable.Range = Range(0, 1, 2, 3, 4, 5) scala> Random.shuffle(indices) <console>:10: 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]. Random.shuffle(indices)