Skip to content

Instantly share code, notes, and snippets.

@lrytz
Created March 26, 2019 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrytz/fe09791208969f07e211c5c8601db125 to your computer and use it in GitHub Desktop.
Save lrytz/fe09791208969f07e211c5c8601db125 to your computer and use it in GitHub Desktop.
import annotation.unchecked.uncheckedVariance
trait Builder[-A, +To]
trait IterableFactory[+CC[_]] {
def from[A](source: It[A]): CC[A]
def newBuilder[A]: Builder[A, CC[A]]
}
trait ItOps[+A, +CC[_], +C] {
protected type IterableCC[X] = CC[X] @uncheckedVariance
protected type IterableC = C @uncheckedVariance
protected def newSpecificBuilder: Builder[A @uncheckedVariance, IterableC]
protected def fromSpecific(coll: It[A @uncheckedVariance]): IterableC
def iterableFactory: IterableFactory[IterableCC]
def foo: C = fromSpecific(null)
}
trait It[+A] extends ItOps[A, It, It[A]] {
protected def fromSpecific(coll: It[A @uncheckedVariance]): IterableC = iterableFactory.from(coll)
protected def newSpecificBuilder: Builder[A @uncheckedVariance, IterableC] = iterableFactory.newBuilder[A]
def iterableFactory: IterableFactory[IterableCC] = It
}
object It extends IterableFactory[It] {
def from[A](source: It[A]): It[A] = new It[A]{}
def newBuilder[A]: Builder[A,It[A]] = new Builder[A, It[A]]{}
}
trait SqOps[A, +CC[_], +C] extends ItOps[A, CC, C]
trait Sq[A] extends It[A] with SqOps[A, Sq, Sq[A]] {
def flup = 0
}
object Test {
def main(args: Array[String]): Unit = {
val sq = new Sq[String] { }
sq.foo.flup
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment