In my project, I have a type A
, used for arguments in a few places, where I want a bunch of types automatically converted to that type. I've implemented this using a few implicit classes in the companion object of A
. I've removed everything not necessary to trigger the problem:
trait A
object A {
implicit class SeqA[T](v: Seq[T])(implicit x: T => A) extends A
implicit class IntA(v: Int) extends A
implicit class TupleA(v: (Int, Int)) extends SeqA(Seq(v._1, v._2))
}
But scalac
rejects this code because of an illegal cyclic reference:
$ scalac -version
Scala compiler version 2.12.8 -- Copyright 2002-2018, LAMP/EPFL and Lightbend, Inc.
$ scalac A.scala
A.scala:5: error: illegal cyclic reference involving class TupleA
implicit class TupleA(v: (Int, Int)) extends SeqA(Seq(v._1, v._2))
^
one error found
What exactly is the problem here? What is the compiler tying to do/infer/resolve that involves an illegal cylce?
Bonus points for a valid way to implement these implicit classes.
SeqA
after I already hadTupleA
, that's why I didn't notice that a type parameter was inferred there. – Wigwag