Import Scala companion object to the file where it is defined

I have a ThisThing.scala file containing:

class ThisThing() extends BaseThing[ThisEvent, ThisState]
object ThisThing {
  sealed trait ThisEvent
  case class Load(uuid: UUID) extends ThisEvent
  case object Stop extends ThisEvent

  sealed trait ThisState
  case object Loading extends ThisState
  case object Loaded extends ThisState
}

In cases where ThisThing needs to be parameterized with the types of events and the states that it processes. This also requires import:

import ThisThing._ 

Which is really strange, you need to import something from the same file. I assume that I am doing something non-idiomatic and that there is a better way to structure it?

+4
source share
1 answer

This is a little strange, but basically valid.

It neidiomatichno, because you're trying to hide ThisEvent, and ThisStatein ThisThingthe accompanying object, but at the same time, expose these types of signature ThisThing ... extends BaseThing[ThisEvent, ThisState].

, , ThisThing ThisThing -, BaseThing[ThisEvent, ThisState]:

// much later, in someone else code in a galaxy far, far away 
import foo.bar.baz.ThisThing.{ThisEvent, ThisState} // awkward to use!
val t: BaseThing[ThisEvent, ThisState] = new ThisThing

... , - ThisEvent ThisState , ThisThing.

, , ThisThing BaseThing[X, Y], , , ThisThing , , ThisThing, BaseThing - ThisThing BaseThing .

, :

  • 1: BaseThing[ThisEvent, ThisState] . ThisEvent ThisState .
  • 2: BaseThing[ThisEvent, ThisState] - .

: - Akka FSM, , . , , , . ThisThing, , - , ActorRef s, . " " , FSM.

+2

Source: https://habr.com/ru/post/1693940/


All Articles