Skip to content
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

BuildFrom inference fails for Set subclass extending AbstractSet #13069

Open
lrytz opened this issue Dec 13, 2024 · 2 comments
Open

BuildFrom inference fails for Set subclass extending AbstractSet #13069

lrytz opened this issue Dec 13, 2024 · 2 comments
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) library:collections
Milestone

Comments

@lrytz
Copy link
Member

lrytz commented Dec 13, 2024

This is a simplification of Enumeration#ValueSet which has the same issue.

package scala

import collection.immutable
import collection.mutable
import collection.BuildFrom

abstract class MySet
  extends /*immutable.AbstractSet[String]
    with*/ immutable.Set[String]
    with immutable.SetOps[String, immutable.Set, MySet] {
  override protected def fromSpecific(coll: IterableOnce[String]): MySet = ???
  override protected def newSpecificBuilder: mutable.Builder[String, MySet] = ???
  override def empty: MySet = ???
}


class SetTest {
  implicit class MappExt[A, CC <: Iterable[A]](c: CC with collection.IterableOps[A, collection.AnyConstr, CC]) {
    def mapp[B, That](f: A => B)(implicit cbf: BuildFrom[CC, B, That]): That = {
      val b = cbf.newBuilder(c)
      c.foreach(b += f(_))
      b.result()
    }
  }

  // these fail with the `AbstractSet` parent
  def t(s: MySet) = s.mapp(_ => 1)
  implicitly[BuildFrom[MySet, String, immutable.Set[String]]]
  implicitly[BuildFrom[MySet, String, immutable.Set[String]]](BuildFrom.buildFromIterableOps)
  
  // works also with `AbstractSet`
  implicitly[BuildFrom[MySet, String, immutable.Set[String]]](BuildFrom.buildFromIterableOps[immutable.Set, String, String])

  // original example
  En.values.mapp(_.toString)
}

object En extends Enumeration {
  val X = Value("x")
}

This compiles, but when un-commenting the AbstractSet parent it fails.

When changing MySet to MySet[T] and adjusting the immutable.SetOps[String, immutable.Set, MySet] parent to immutable.SetOps[String, MySet, MySet[T]], things work also with the AbstractSet parent.

Maybe this is a puzzle that can be solved, I didn't manage so far.

@lrytz lrytz added library:collections fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) labels Dec 13, 2024
@lrytz
Copy link
Member Author

lrytz commented Dec 13, 2024

It works in Scala 3, including the Enumeration example. So probably it's a compiler / inference issue, not a library puzzle.

@SethTisue
Copy link
Member

@scala/collections

@SethTisue SethTisue added this to the Backlog milestone Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) library:collections
Projects
None yet
Development

No branches or pull requests

2 participants