scala assign value to object

我们两清 提交于 2019-12-26 06:48:08

问题


I have the following code:

 def getContentComponents: Action[AnyContent] = Action.async {
        contentComponentDTO.list().map(contentComponentsFuture =>
          contentComponentsFuture.foreach(contentComponentFuture =>

            contentComponentFuture.typeOf match {
              case 5 =>
                contentComponentDTO.getContentComponentText(contentComponentFuture.id.get).map(
                  text => contentComponentFuture.text = text.text
                )
            }
          )
            Ok(Json.toJson(contentComponentsFuture))
        )

and get this error message while assigning a value:

Is there a way to solve this issue?

I thought about creating a copy but that would mean that I have do lots of other stuff later. It would be much easier for me if I could reassign the value.

thanks


回答1:


Unfortunately this is not possible as contentComponentFuture.text is an immutable property so you cannot change its value like that.

The only way to "change" the values of the job object is to actually create a totally new instance and discard the old one. This is standard practice when dealing with immutable objects.

Sorry for the bad news.




回答2:


Just found the solution ...

In model I have to define the Attributes as var

case class ContentComponentModel(
                              id: Option[Int] = None,
                              typeOf: Int,
                              name: String,
                              version: String,
                              reusable: Option[Boolean],
                              createdat: Date,
                              updatedat: Date,
                              deleted: Boolean,
                              processStepId: Int,
                              var text: Option[String],


来源:https://stackoverflow.com/questions/45835422/scala-assign-value-to-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!