使用Scaldi时编译错误

时间:2014-12-30 04:19:41

标签: scala dependency-injection scaldi

我正在遵循Scaldi documentaiton中提到的步骤。以下是我的代码。

class Game(players: List[Player], currentPlayer: Player,
           board: Board, active: Boolean, gamePersistor: GamePersistor) extends Injectable {

  def this(players: List[Player], currentPlayer: Player,
           board: Board, active: Boolean)(implicit inj: Injector) {
    this(players, currentPlayer, board, active, inject[GamePersistor])
  }
}

我收到以下编译错误。

Error:(11, 49) not found: value inject
    this(players, currentPlayer, board, active, inject[GamePersistor])
                                                ^

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

从文档

中找出

所有形式的注入期望和Injector的隐式实例都在范围内。如果

  

您正在注入模块定义,然后它已经提供了   一个给你。如果你注入自己的课程,那么最好的   方法是提供隐式注入器实例作为   构造函数参数,如上例所示。

所以代码应该是

class Game(players: List[Player], currentPlayer: Player,
           board: Board, active: Boolean, gamePersistor: GamePersistor)(implicit inj:Injector) extends Injectable {

  def this(players: List[Player], currentPlayer: Player,
           board: Board, active: Boolean)(implicit inj: Injector) {
    this(players, currentPlayer, board, active, inject[GamePersistor])
  }
}