Skip to content

BuildFrom inference fails for Set subclass extending AbstractSet #13069

Open
@lrytz

Description

@lrytz

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)library:collections

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions