You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: overviews/collections/arrays.md
+11-10
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ languages: [ja, zh-cn]
18
18
scala> val a3 = a2 filter (_ % 2 != 0)
19
19
a3: Array[Int] = Array(3, 9)
20
20
scala> a3.reverse
21
-
res1: Array[Int] = Array(9, 3)
21
+
res0: Array[Int] = Array(9, 3)
22
22
23
23
Given that Scala arrays are represented just like Java arrays, how can these additional features be supported in Scala? In fact, the answer to this question differs between Scala 2.8 and earlier versions. Previously, the Scala compiler somewhat "magically" wrapped and unwrapped arrays to and from `Seq` objects when required in a process called boxing and unboxing. The details of this were quite complicated, in particular when one created a new array of generic type `Array[T]`. There were some puzzling corner cases and the performance of array operations was not all that predictable.
24
24
@@ -29,7 +29,7 @@ The Scala 2.8 design is much simpler. Almost all compiler magic is gone. Instead
29
29
scala> val a4: Array[Int] = seq.toArray
30
30
a4: Array[Int] = Array(1, 2, 3)
31
31
scala> a1 eq a4
32
-
res2: Boolean = true
32
+
res1: Boolean = true
33
33
34
34
The interaction above demonstrates that arrays are compatible with sequences, because there's an implicit conversion from arrays to `WrappedArray`s. To go the other way, from a `WrappedArray` to an `Array`, you can use the `toArray` method defined in `Traversable`. The last REPL line above shows that wrapping and then unwrapping with `toArray` gives the same array you started with.
35
35
@@ -74,7 +74,8 @@ The `evenElems` method returns a new array that consist of all elements of the a
74
74
75
75
error: cannot find class manifest for element type T
76
76
val arr = new Array[T]((arr.length + 1) / 2)
77
-
^
77
+
^
78
+
78
79
What's required here is that you help the compiler out by providing some runtime hint what the actual type parameter of `evenElems` is. This runtime hint takes the form of a class manifest of type `scala.reflect.ClassManifest`. A class manifest is a type descriptor object which describes what the top-level class of a type is. Alternatively to class manifests there are also full manifests of type `scala.reflect.Manifest`, which describe all aspects of a type. But for array creation, only class manifests are needed.
79
80
80
81
The Scala compiler will construct class manifests automatically if you instruct it to do so. "Instructing" means that you demand a class manifest as an implicit parameter, like this:
@@ -102,15 +103,15 @@ Here is some REPL interaction that uses the `evenElems` method.
102
103
103
104
In both cases, the Scala compiler automatically constructed a class manifest for the element type (first, `Int`, then `String`) and passed it to the implicit parameter of the `evenElems` method. The compiler can do that for all concrete types, but not if the argument is itself another type parameter without its class manifest. For instance, the following fails:
104
105
105
-
scala> def wrap[U](xs: Array[U]) = evenElems(xs)
106
-
<console>:6: error: could not find implicit value for
107
-
evidence parameter of type ClassManifest[U]
108
-
def wrap[U](xs: Array[U]) = evenElems(xs)
109
-
^
106
+
scala> def wrap[U](xs: Vector[U]) = evenElems(xs)
107
+
<console>:6: error: No ClassManifest available for U.
108
+
def wrap[U](xs: Vector[U]) = evenElems(xs)
109
+
^
110
+
110
111
What happened here is that the `evenElems` demands a class manifest for the type parameter `U`, but none was found. The solution in this case is, of course, to demand another implicit class manifest for `U`. So the following works:
This example also shows that the context bound in the definition of `U` is just a shorthand for an implicit parameter named here `evidence$1` of type `ClassManifest[U]`.
0 commit comments