Skip to content

No BuildFrom for IArray #19229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bishabosha opened this issue Dec 9, 2023 · 9 comments
Open

No BuildFrom for IArray #19229

bishabosha opened this issue Dec 9, 2023 · 9 comments
Labels

Comments

@bishabosha
Copy link
Member

Compiler version

3.3.1

Minimized code

def diffs(ns: IArray[Long]): IArray[Long] =
  ns.lazyZip(ns.tail).map((a, b) => b - a)

Output

Cannot construct a collection of type C with elements of type Long based on a collection of type IArray[Long]..
I found:

    scala.collection.BuildFrom.buildFromIterableOps[CC, A0, A]

But method buildFromIterableOps in trait BuildFromLowPriority2 does not match type scala.collection.BuildFrom[IArray[Long], Long, C]

where:    C is a type variable with constraint <: IArray[Long]

Expectation

compiles

@bishabosha bishabosha added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label area:library Standard library and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Dec 9, 2023
@SethTisue
Copy link
Member

something someone from @scala/collections might like to tackle?

@bishabosha
Copy link
Member Author

Another annoyance is IArray doesn't have a Factory so no .to(IArray)

@bishabosha
Copy link
Member Author

bishabosha commented Dec 2, 2024

with advent of code 2024 I am again bitten here, relevant methods to copy/cast the Array implementation from:

  • scala.collection.BuildFrom.buildFromArray
  • Array.toFactory

@Ichoran
Copy link

Ichoran commented Dec 2, 2024

I wouldn't bother with IArray for normal use. It's nice in principle that one can declare that it should be immutable, but in practice there's so much friction with the rest of existence that it's not really a win. Obviously if anyone wants to try to get it better integrated, that's fine, but I think basically it's an idea that doesn't pan out as well in practice as one might hope.

@bishabosha
Copy link
Member Author

what way do you mean? is that more a general statement on opaque type wrappers? or specifically an issue with IArray?

@Ichoran
Copy link

Ichoran commented Dec 2, 2024

Array is really deeply enmeshed with the entire ecosystem, both in Scala and Java. Because of that, the surface area that you need to cover to get an IArray that works comparably is quite large, and there remain many points of friction.

In cases where you have opaque types where the point is to generate a different interface, that isn't as much of an issue: you don't want to pick up the behavior of the underlying type. That's the point!

But with IArray, you want to pick out a small subset of a huge number of behaviors of Array that you don't obey (the mutable ones), and everything else you want to work the same (much like mutable.Seq vs immutable.Seq). It's a ton of work.

Thus, I think pragmatically, the better approach is just to accept Array as a special snowflake, and maintain appropriate caution. There are lots of areas where the type system can't help you. Even though it might be able to here, I think it's easier, usually, to declare that this is a case where it could but isn't worth the effort.

In fact, I'd expect the easier interface to be having a general-purpose box Imm[A] that marks that whatever is inside shouldn't be mutated any longer.

But I have odd preferences sometimes. Maybe IArray is the way to go for not-like-me people. But I tried it pretty extensively, and found that for me the cognitive overhead of maintaining two interfaces was considerably worse than the cognitive overhead of just being careful around Array.

@bishabosha
Copy link
Member Author

In fact, I'd expect the easier interface to be having a general-purpose box Imm[A] that marks that whatever is inside shouldn't be mutated any longer.

coming soon™️ with caprese https://infoscience.epfl.ch/entities/publication/0d93a16f-f957-4c40-99db-43ed1301dff3

@ritschwumm
Copy link

@Ichoran funny - i am quite happy with IArray. almost everything i need, and none of the disconcerning
mutability :)

@Ichoran
Copy link

Ichoran commented Dec 3, 2024

@ritschwumm - Well, like I said, my tastes do not match everyone's tastes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants