Skip to content

Class cast exception when implementing custom collection #7325

Closed
@kavedaa

Description

@kavedaa

minimized code

The following compiles in Dotty 0.19.0-RC1:

class MySeq[A](private val underlying: Seq[A])
  extends Seq[A] 
  with SeqOps[A, MySeq, MySeq[A]] {
  def apply(n: Int) = underlying(n)
  def iterator = underlying.iterator
  def length = underlying.size
}

but gives ClassCastException at runtime:

scala> new MySeq(Seq(1, 2, 3))
val res0: test.MySeq[Int] = Seq(1, 2, 3)

scala> res0 map(_ + 1)
java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to test.MySeq

expectation

In Scala 2.13.1 it does not compile:

[error] [...] incompatible type in overriding
[error] def iterableFactory: scala.collection.IterableFactory[[_]test.MySeq[_]] (defined in trait IterableOps)
[error]   with override def iterableFactory: scala.collection.SeqFactory[scala.collection.Seq] (defined in trait Seq);
[error]  found   : => scala.collection.SeqFactory[scala.collection.Seq]
[error]  required: => scala.collection.IterableFactory[[_]test.MySeq[_]];
[error]  other members with override errors are: fromSpecific, newSpecificBuilder, empty
[error] class MySeq[A](private val underlying: Seq[A])

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions