joesan
joesan

Reputation: 15345

Scala Type Alias In Object

I have an Object that has some type alias definitions:

case class MyCaseClass(someType: SomeType)
object MyObject extends SomeType {

  type SomeType = Int => Boolean
}

IntelliJ tells me that I have to import SomeType so that my case class can see it, but later the compiler is not happy and complains about illegal cyclic reference on the import statements. I have a couple of other objects that are declared as below:

object MyModule {
 object Obj1 { ... }
 object Obj2 { ... }
 object Obj3 { ... }
}

Each of those Obj's refer to the type alias defined in MyObject.

Is there a best practice to encapsulate the types? Should I move them in a trait or would that be considered an abuse? Suggestions?

Upvotes: 0

Views: 222

Answers (1)

Sascha Kolberg
Sascha Kolberg

Reputation: 7152

You object MyObject tries to inherit from a type that itself defines.

object MyObject extends SomeType {
  type SomeType = Int => Boolean
}

So, the first thing to do is define it at least outside MyObject:

type SomeType = Int => Boolean
object MyObject extends SomeType {
  def apply(v1: Int): Boolean = ???
}

Now, as scarce the information in your question is, I'd suggest you think a bit about your design. Identify where SomeType is referenced, where it needs to be visible. After that you could, for example, put it into a package object at a package level where it is visible to all referencing parties.

Upvotes: 3

Related Questions