Skip to content

Class cast exception when implementing custom collection #7325

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

Closed
kavedaa opened this issue Sep 26, 2019 · 0 comments
Closed

Class cast exception when implementing custom collection #7325

kavedaa opened this issue Sep 26, 2019 · 0 comments

Comments

@kavedaa
Copy link

kavedaa commented Sep 26, 2019

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])
smarter added a commit that referenced this issue Jun 22, 2021
changvvb pushed a commit to changvvb/dotty that referenced this issue Jun 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants