Giorgio
Giorgio

Reputation: 5173

`illegal cyclic reference` error in Eclipse / Scala plugin

I have created a folder x with a source file package.scala in it. The file contains the following code:

package x

package object y
{
  trait A

  case class B extends A
}

Eclipse displays a compilation error: illegal cyclic reference involving object y.

I am not quite sure what this means. If I try to define a normal class (removing case) the error message disappears. Another solution is to use a normal object instead of a package object:

package x

object y
{
  trait A

  case class B extends A
}

Where is the cyclic reference? And how can I remove it? As far as I understand I cannot define a trait and a case subclass of it inside a package object. But I have not clue as to where this restriction comes from.

Upvotes: 5

Views: 1319

Answers (1)

Mark Butler
Mark Butler

Reputation: 4391

As PedroFuria says this is a bug, but it's always good to look at the issue associated with the bug in these situations. Specifically the conclusion was back in September 2011:

Defining classes in package objects is only half-working in the Scala compiler itself. You can work around this by defining your class in the right directory. I doubt this will be fully supported any time soon.

So best to refer to this related issue which points out is easy to replace:

package object mypkg {
  class MyClass
}

with the following:

package mypkg {
  class MyClass
}

Which will work with Eclipse / EclipseIDE.

Upvotes: 2

Related Questions