AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Scala array4/7/2023 To illustrate the problem, consider the following attempt to write a generic method that creates an array.I am a newbie to functional programming language and I am learning it in Scala for a University project. This is an even harder problem, which requires a little bit of help from you. Representing the generic array type is not enough, however, there must also be a way to create generic arrays. This means that if you need maximal performance, you should prefer concrete over generic arrays. You can expect accesses to generic arrays to be three to four times slower than accesses to primitive or object arrays. These type tests slow down array operations somewhat. At run-time, when an element of an array of type Array is accessed or updated there is a sequence of type tests that determine the actual array type, followed by the correct array operation on the Java array. The only common run-time type encompassing all of these types is AnyRef (or, equivalently ), so that’s the type to which the Scala compiler maps Array. How then is Scala’s Array represented? In fact a generic array like Array could be at run-time any of Java’s eight primitive array types byte, short, char, int, long, float, double, boolean, or it could be an array of objects. What about genericity? In Java you cannot write a T where T is a type parameter. So now you know how arrays can be compatible with sequences and how they can support all sequence operations. So if both conversions are applicable, the one in Predef is chosen. Implicits in subclasses and subobjects take precedence over implicits in base classes. The first is defined in the Predef object whereas the second is defined in a class scala.LowPriorityImplicits, which is inherited by Predef. The ArrayOps conversion has a higher priority than the WrappedArray conversion. The answer to that question is that the two implicit conversions are prioritized. After all, both conversions map an array to a type that supports a reverse method, which is what the input specified. This raises the question how the compiler picked intArrayOps over the other implicit conversion to WrappedArray in the line above. Where intArrayOps is the implicit conversion that was inserted previously. The difference between the two implicit conversions on arrays is shown in the next REPL dialogue: Modern VMs often avoid creating this object entirely. Typically, this ArrayOps object is short-lived it will usually be inaccessible after the call to the sequence method and its storage can be recycled. “Adding” means that the array is wrapped in another object of type ArrayOps which supports all sequence methods. This conversion simply “adds” all sequence methods to arrays but does not turn the array itself into a sequence. There is yet another implicit conversion that gets applied to arrays. The last REPL line above shows that wrapping and then unwrapping with toArray gives the same array you started with. To go the other way, from a WrappedArray to an Array, you can use the toArray method defined in Traversable. The interaction above demonstrates that arrays are compatible with sequences, because there’s an implicit conversion from arrays to WrappedArrays. Instead there is an implicit “wrapping” conversion between arrays and instances of class, which is a subclass of Seq. It can’t really be that because the data type representation of a native array is not a subtype of Seq. In Scala 2.8 an array does not pretend to be a sequence. Instead the Scala 2.8 array implementation makes systematic use of implicit conversions. There were some puzzling corner cases and the performance of array operations was not all that predictable. The details of this were quite complicated, in particular when one created a new array of generic type Array. Previously, the Scala compiler somewhat “magically” wrapped and unwrapped arrays to and from Seq objects when required in a process called boxing and unboxing. 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.
0 Comments
Read More
Leave a Reply. |