As described in scala collections docs, IsSeq, IsIterable etc allow truly generic collection-style algorithms:
I would propose to provide an instance for IArray (to accompany the Array implementation), this one will be based on the immutable.ArraySeq class for widening operations (otherwise returns IArray e.g. for .filter).
Implementation here: Add an implementation of IsSeq for IArray. by bishabosha · Pull Request #24552 · scala/scala3 · GitHub
1 Like
Could you add an example of what was not possible before, but is now possible with your change? How does it help the users? It would help a lot to get a full picture here.
1 Like
taking the example from the docs, here is a function that you can define currently that will not work for IArray, but will work after the change. (it works for standard Array)
extension [Repr](repr: Repr)(using iter: IsIterable[Repr])
def sumBy[B: Numeric](f: iter.A => B): B =
// from https://docs.scala-lang.org/overviews/core/custom-collection-operations.html
val coll = iter(repr)
val it = coll.iterator
var result = f(it.next())
while it.hasNext do
result = summon[Numeric[B]].plus(result, f(it.next()))
result
case class Rational(num: Int, denom: Int):
def toDecimal = BigDecimal(num) / denom
Seq(Rational(1,2), Rational(1,4)).sumBy(_.toDecimal) // ok
IArray(Rational(1,2), Rational(1,4)).sumBy(_.toDecimal) // error
1 Like