I have recently migrated the codebase from Vector to IArray, because it is the perfect collection for so many use-cases. I have encountered many quirks and have reported the bugs to you. But there is one major one remaining.
Here I propose two improvements:
1: Spread matching should return IArray, not ArraySeq
array match {
case IArray(a, middle*, b) => // middle is ArraySeq
}
The problem is not just the ArraySeq wrapper object, but how difficult is it to convert ArraySeq back to IArray, especially because the arrays of primitives have their specifics. I think the code is, but it might be wrong:
Imagine writing this every time, it is not pretty.
2: Support :+ destructuring
Since the multiSpreads SIP-70 improvement, this is less esential, but it would be a nice win for ortnogonality, especially when you are migrating the codebase from Vector.
I have heard an objection in the other thread that this might not be very performant, but to do it once in a while, it is okay. Assuming it uses System.arrayCopy under the hood, it beats all other collection types that support :+:
ArraySeq: Same performance
Vector: Might be very much slower for small collections of under 100 elements.
List: Absolutely terrible performance, yet it is allowed.
Ah, this is quite disappointing that is has to be a Seq.
A zero-copy IArray.from specialization for ArraySeq sounds nice. Both are immutable, so that should be safe. And JDK JIT might be smart enough to eliminate the allocation.
IArray has some more catches like poor == (equality) and poor .toString.
some tried to migrate the compiler from List to IArray in #26391, but later on switched to value classes (class Lst[+A](_: IArray[?<: A]) extends AnyVal) instead. best of both worlds.
Lst is now a value class instead of an opaque type alias. This way can get == , hashCode and toString with reasonable semantics.
Very interesting and unique approach, how did you even think of that?
I thought we are discouraging the use of AnyVal in Scala 3. There has been some previous discussion here how to introduce equals and hashCode and toString to opaque types. Should be possible. But yes, it is a problem, you can have IArray as a field in case class and suddenly equality does not work anymore.
Did you observe any disadvantages of your approach whatsoever? For example that the compiler failed to eliminate the wrapper somewhere? Or any strange issues regarding ClassTag. Does your wrapper work for IArray[T] where T is an opaque type of a primitive?
It’s all the usual disadvantages of AnyVal, of course. For instance, Lst[Lst[Int]] boxes the inner Lst. Any time you touch generic code, Lst[Int] goes into a box. IArray[Int] doesn’t, but you lose the ability to tell whether it is Array[Int] or IArray[Int] at runtime.
this is impossible to support parametrically, any time your opaque type value is stored into a variable of a wider type than it is (e.g. in default HashMap) then it would need to be boxed in a class that could point to those methods, so making the design reason (no boxing) for using opaque type useless.
unless you can opt out of ever widening the type?
it is unlikely that would pass, so type class based is really the way to go if you want a string for a specific purpose
wrap with AnyVal, where unboxing is a conservative best effort, boxing whenever there is any risk you would actually have to pattern match against the wrapper type, or otherwise check its runtime type (which means a lot)
using opaque type where there is no wrapper in the runtime, and you also cannot distinguish between the underlying type an opaque type - any opaque type of that underlying type! Since JVM does not distinguish them it has no way of overriding the behavior (because the only way to override a behavior is to wrap or create a subtype, and opaque type does neither)
It’s either enforcing types only in compile time, with no reliance of dynamic dispatch (passing type class around in an exception, but it’s still the compiler that takes care of providing the right instance to its use site) or a runtime type checking, or an overhead of boxing that carries the type information. A “smart approach” to this was tried (AnyVal magic) and it couldn’t be done good enough despite years of effort.
“Overriding defs” in opaque types would disallow virtually any type parametric type:
Option/Some checks equality by checking if values inside it returns true for equals - impossible to override without wrapping
same is true for: Either/Left/Right
and for comparing any collection (Seq/List/Vector)
If you used hash-based collections, they would look at the hashcode inherited from Object, also impossible to override without wrapping.
Printing any of these? toString from Object, you either wrap it or use a subtype that overrides it.
From AnyVal pitfalls there was also the need to wrap in runtime if one passed a value to Function1 (or any function) because you could always implement it as
val: Foo => Bar = {
case Foo(value) => ...
}
“Widening the type” is not the constraint. AFAIR any type parametric class would require you to wrap, because in the runtime there would be some bytecode after type erasure, that has to distinct value type from a raw primitive, and needs a runtime information for it. Also, any code already compiled, which uses equals/toString/hashcode/etc, would not suddenly learn that for some particular type it should not use these methods but call something else. It would call these methods defined on java.lang.Object, and the only way to override these would be to override def in a subtype of this class - which in the idea above require wrapping it in runtime.
I don’t remember all the details from AnyVal gotchas, but being able to “override defs” in such a way, without wrapping, would only be possible in monomorphic code where compiler could always know the exact runtime and compile time type, and only in the code that already know this type. That eliminates:
all collections
all tuples
all functions/lambdas
all other generic utilities from std (e.g. Option/Either/Try) and 3rd party libraries (e.g. NonEmpty* from Cats)
I wonder in which use case the sweet spot is that having a bare Array is required over an ArraySeq, but it’s also required to have an immutable view on that Array via IArray and all the quirks and gotchas of IArray don’t matter.
From what I can see the actual implementation in that PR uses a regular Array of Object.
class Lst[+T](private val arr: Array[Object]) extends AnyVal
@scalafan You could take a look at my code found here: openstrat/Util/srcArr the main driver for this was to create collections for sequences of points, line segments, polygons, grid positions etc. You might notice that the RArr class, essentially an immutable array for reference objects does not extend AnyVal. It used to, and this worked for JVM and JavaScript in Scala 2, however it does not work for Scala.js in Scala 3 and unfortunately the compiler team have said they won’t fix Avoid bridge clashes involving value classes for anonymous classes #11264
So for simplicity I removed the extension from AnyVal so as Js and Jvm could share the same file, and its in the browser that you are most resource limited. The code in that directory uses builders allowing you to map to and between these immutable sequence collections. But there’s also builders for mapping to and between polygons and linepaths in different geometries in the Geom module.
I also found that ArraySeq and Opaque types were not really useful.