question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Illegal cyclic reference error when abstract companion

See original GitHub issue

I suppose the companion objects aren’t working as expected when they have a type hierarchy. Consider a simple example:

  1. I have some re-usable companion:
abstract class AbstractObjectCompanion[T] {
  def apply(src: Int): T
  implicit class Ops(t: T) {
    def print = t.toString
  }
}
  1. And its implementation here:
import io.estatico.newtype.macros.newtype

package object some {
  @newtype class MyInt(val i: Int)
  object MyInt extends AbstractObjectCompanion[MyInt] {
    override def apply(src: Int): MyInt = new MyInt(src)
  }
}

… does not compile:

illegal cyclic reference involving type MyInt
  object MyInt extends AbstractObjectCompanion[MyInt] {

But it compiles without @newtype OR without AbstractObjectCompanion.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
hmemcpycommented, Dec 21, 2021

D’oh! Of course 😃 Haha this is even better! Thanks @joroKr21!

1reaction
dwijnandcommented, Feb 15, 2021

Out of interest I looked at this, the macro desugars to:

package object some {
  type MyInt = MyInt.Type
  object MyInt extends AbstractObjectCompanion[MyInt] {
    type Base = Any { type __MyInt__newtype }
    abstract trait Tag extends Any
    type Type <: Base with Tag
  }
}

(ignoring other details) which is just cyclic. I guess Base/Tag could be obfuscated a bit (__MyInt__ prefixed) and moved out, and just define the external Type.

I’m guessing that the compiler must have ways to deal with this when dealing with companion classes and companion objects in a cyclical relationship…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · estatico/scala-newtype - GitHub
Illegal cyclic reference error when abstract companion. #67 opened on Feb 15, 2021 by dmitry-worker · 5. No TypeTag available for newtypes.
Read more >
Explanation for "illegal cyclic reference" involving implicits
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....
Read more >
scalaz/scalaz-plugin - Gitter
_ abstract class AnnotatedClassTransformer(predicate: Symbol => Boolean, ... Symbols$CyclicReference: illegal cyclic reference involving method copy: while ...
Read more >
[SOLVED] Objects implementing traits - Question - Scala Users
I have there that problem where a Companion Object extends a trait and it ... Error:(10, 56) illegal cyclic reference involving object Store ......
Read more >
Web service error codes (Microsoft Dataverse) - Power Apps
Message: Field {0} cannot be used in calculated field {1} because it would create a circular reference. 0x8006043a -2147089350, Name: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found