意外错误:重新分配给val

时间:2015-06-02 12:59:39

标签: scala

编译时出现此错误:

Compilation error: reassignment to val

我认为这似乎很明显,但在查看我的代码时,我找不到为什么这不起作用。

以下是发生错误的代码示例,来自我的ConferenceController.scala:

def createConfWithPrivacy(confId: Long, groupId: Option[Long]) = ForcedAuthentication 
{implicit request =>
    Future {
      val user = request.user.get
      var newConf = Conference.findById(confId).get
      if(groupId.isDefined){
        newConf.forGroup = LabGroup.findById(groupId.get)  <- there it is
        Conference.updateForGroup(newConf)
      }
      ...
    }
}

Conference.scala中的变量声明如下:

case class Conference (
    id         : Long,
    title      : String,
    abstr      : String,
    speaker    : Speaker,
    startDate  : DateTime,
    length     : Duration,
    organizedBy: Lab,
    location   : Location,
    accepted   : Boolean,
    acceptCode : Option[String],
    priv       : Boolean,
    forGroup   : Option[LabGroup]=None
) {... Functions ...}

调用的函数来自LabGroup.scala文件,我知道这个函数有效:

def findById(id: Long): Option[LabGroup] = DB.withConnection { implicit  c =>
    SQL("SELECT * FROM LabGroup WHERE id = {id}")
      .on("id" -> id)
      .as(labGroupParser.singleOpt)
  }

但是我不明白为什么会出现这个错误,因为newConf是一个var,通常Conference.forGroup也应该是一个。

如果你看错了,请告诉我。

感谢您的帮助。

2 个答案:

答案 0 :(得分:5)

var newConf = Conference.findById(confId).get定义变量newConf。可以重新分配此变量。但是,forGroup的{​​{1}} 仍然只是一个值,因此无法更改。

如果您要为newConf更新forGroup,则需要将其声明为变量,您可以执行以下操作:

newConf

答案 1 :(得分:4)

当你实例化你已经完成的案例类时,默认情况下它的每个成员都是 val 。您必须明确告诉Scala您希望将其作为 var 。就这样做:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Services;

namespace AutoCompleteDeneme
{

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX,      uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class PersonelService : System.Web.Services.WebService
{

    [WebMethod]
    public List<string> GetPersonelNames(string personelName)
    {

        string CS =   ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
        List<string> personelNames = new List<string>();
        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("spGetMatchingPersonelNames", con);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlParameter parameter = new SqlParameter("@PersonelName", personelName);
            cmd.Parameters.Add(parameter);
            con.Open();
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                personelNames.Add(rdr["adi"].ToString());

            }
        }
        return personelNames;


    }
}
}

修改
正如Régis Jean-Gilles在他的评论中发布的那样,更加惯用的方法是更改​​ forGroup 的值,这就是以下代码:

  

newConf.forGroup = newConf.copy(forGroup = LabGroup.findById(groupId.get))

这会使forGroup成为,并且是一种更好的[功能性]方式来做&#34;重新分配&#34;。