Skip to content
This repository has been archived by the owner on Dec 22, 2021. It is now read-only.

Writing simple subclasses used to be simpler #379

Closed
lrytz opened this issue Jan 27, 2018 · 2 comments
Closed

Writing simple subclasses used to be simpler #379

lrytz opened this issue Jan 27, 2018 · 2 comments

Comments

@lrytz
Copy link
Member

lrytz commented Jan 27, 2018

In 2.12:

scala>   class K extends Iterable[Int] {
     |     var x = 10
     |     def iterator: Iterator[Int] = new Iterator[Int] {
     |       def hasNext = x > 0
     |       def next() = { x -= 1; x}
     |     }
     |   }
defined class K

scala>

scala> (new K).toList
res0: List[Int] = List(9, 8, 7, 6, 5, 4, 3, 2, 1, 0)

In strawman I need to add

    def iterableFactory: collection.IterableFactoryLike[Iterable] = Iterable
    protected[this] def fromSpecificIterable(coll: scala.Iterable[Int]): Iterable[Int] = iterableFactory.from(coll)
    protected[this] def newSpecificBuilder(): collection.mutable.Builder[Int, Iterable[Int]] = iterableFactory.newBuilder()

It's pretty mechanical (and also not obvious to newcomers) in the basic case when you don't need specific return types for operations. Should we provide some helper classes with default implementations?

@julienrf
Copy link
Contributor

Duplicate of #335. I’ve copy-pasted your comment there.

@lrytz
Copy link
Member Author

lrytz commented Jan 29, 2018

Thanks, I should have found this.

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

No branches or pull requests

2 participants