简体   繁体   English

Scala和Play:如何使用我的隐式对象?

[英]Scala and Play: how can I use my implicit object?

Implicit parameters should be easy, but so far they're not. 隐式参数应该很容易,但到目前为止还不是。

I've been using ReactiveMongo to save and retrieve data in my Scala / Play 2.4 app. 我一直在使用ReactiveMongo在我的Scala / Play 2.4应用程序中保存和检索数据。 For awhile, I've been doing saves within my Controller, but I want to move the code to the companion object of the entity being saved. 一段时间以来,我一直在控制器中进行保存,但是我想将代码移动到要保存的实体的伴随对象中。 And that's where the trouble has begun. 这就是麻烦开始的地方。

Here are the relevant bits of my Controller, demonstrating how things did work: 这里是我的控制器中的相关内容,展示的东西是怎么的工作:

package controllers

...
import model.Destination
import model.Destination.DestinationBSONReader
import scala.concurrent.ExecutionContext.Implicits.global
...

  def add = Action { implicit request =>
    Destination.destinationForm().bindFromRequest.fold(
      formWithErrors => {
        implicit val helper = sfh(formWithErrors)
        BadRequest(views.html.addDestination(formWithErrors))
      },
      destinationData => {
        Destination.mongoCollection.insert(destinationData)
...
          Redirect("/destinations").flashing("success" -> s"The destination ${destinationData.name} has been created")
        }
      }
    )
  }

You can imagine a form being submitted with data that populated a "Destination", which is then saved to Mongo. 您可以想象一个表单提交的数据填充了“目标”,然后将其保存到Mongo。 The Destination object is imported, as is the scala ExecutionContext. 将导入目标对象,以及scala ExecutionContext。 My "Destination" companion object has an instance of BSONDocumentWriter[Destination] (which is required implicitly by the mongoCollection.insert() call) like so: 我的“ Destination”伴随对象具有BSONDocumentWriter [Destination]的实例(mongoCollection.insert()调用暗含了此实例),如下所示:

object Destination {

  ...

  implicit object DestinationBSONWriter extends BSONDocumentWriter[Destination] {
    override def write(destination: Destination): BSONDocument = {
      val dbId = if (destination.id.isDefined) { destination.id.get } else { BSONObjectID.generate.stringify }
      destination.newId = dbId
      BSONDocument(
          "id" -> dbId,
          ... lots of other key/value pairs,
      "name" -> destination.name)
    }
  }

  ...

}

Now, I want to move that mongoCollection.insert(destinationData) call to that same "Destination" object; 现在,我想将mongoCollection.insert(destinationData)调用移至同一“ Destination”对象; controllers really shouldn't be talking to datastores. 控制器确实不应该与数据存储区通信。 So I created a simple method, which no matter what I do causes compilation errors: 因此,我创建了一个简单的方法,无论做什么都会导致编译错误:

  def create(destination: Destination) = {
    Destination.mongoCollection.insert(destination)
  }

^- compile fails; ^-编译失败; "could not find implicit value for parameter writer: reactivemongo.bson.BSONDocumentWriter[model.Destination]" “找不到参数编写器的隐式值:reactmongo.bson.BSONDocumentWriter [model.Destination]”

So I added imports to the top of the class, since the Scala compiler is supposed to look at imports while searching for implicit: 所以我将导入添加到类的顶部,因为Scala编译器应该在搜索隐式时查看导入:

import model.Destination.DestinationBSONWriter
import scala.concurrent.ExecutionContext.Implicits.global

^- same compilation error ^-相同的编译错误

So I decided to pass the implicit parameters explicitly: 因此,我决定显式传递隐式参数:

  def create(destination: Destination) = {
    Destination.mongoCollection.insert(destination)(writer = DestinationBSONWriter, ec=global)
  }

^- That one causes the compiler to just hang, and I need to restart activator. ^-那导致编译器挂起,我需要重新启动激活器。

I'm baffled. 我很困惑。 That implicit parameter is right there in the same object, but it's not being detected by the compiler. 该隐式参数在同一对象中,但编译器未检测到它。 How am I supposed to use it? 我应该如何使用它?

After a day's worth of looking into this, there is clearly a problem with SBT. 经过一天的研究,SBT显然存在问题。 For whatever reason it's decided to hang whenever it should be successfully compiling my code. 无论出于什么原因,它决定在应该成功编译我的代码时挂起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有两个参数(Scala)的隐式 function? - How can I use an implicit function with two parameters (Scala)? 如何在Play2 Framwork(Scala)中获取请求对象 - How can I get the request object in play2 framwork(scala) 为什么隐式读取找不到我的对象类型scala - Why can implicit read not find my object type scala 如何使用Scala2.10 + Play2.1 + Jerkson? - How can I use Scala2.10+Play2.1+Jerkson? 如何从Scala / Play 2.2项目中使用OrientDB? - How can I use OrientDB from a Scala / Play 2.2 project? 如何在Scala Play 2.5.8中的对象中使用配置 - How to use configuration in scala play 2.5.8 in an object 我可以使用Sclick List和Slick(Play)吗? - Can I use Scala List with Slick (Play)? Scala-播放-如何在Test中模拟导入play.api.libs.concurrent.CustomExecutionContext并将其用作隐式参数? - Scala - Play - How to mock import play.api.libs.concurrent.CustomExecutionContext in Test and use it as an implicit param? 在Scala中,如何使用隐式转换将方法“添加”到公共父类的子类? - In Scala, how can I use implicit conversion to “add” methods to subclasses of a common parent? Playframework 2.2.X scala-如何在json的Writes中使用隐式变量 - Playframework 2.2.X scala - How can I use implicit variable in json's Writes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM