为什么添加println会使我的代码无法编译

时间:2018-09-02 19:12:17

标签: scala

此功能

def updateUser(newValues:User):Future[Option[User]] = {
    println("updating user: "+newValues)
    Future(
    //  println("updating user: "+newValues)//Interestinng. Addinng this makes code uncompilable
    updateOneById(newValues,UserKeys(1,newValues.profile.externalProfileDetails.email))
  )}

如果我取消注释上面的代码,则会收到错误cannot resolve updateOneById

上述方法在以下类中定义

class UsersRepository(session: Session,tablename:String) 
  extends CassandraRepository[UserKeys,User](session, tablename, List("bucket","email")) with UserDao {...}

updateOneByIdCassandraRepository中定义为

def updateOneById(data:M, id: I): Option[M] = {
    println("updating table "+tablename+" with partition key  "+partitionKeyColumns +" and values "+id)
    val resultSet = session.execute(updateValues(tablename,data, id))

    val it = resultSet.iterator();
    if(it.hasNext)
      Some(data) //TODOM should this be Some(rowToModel(it.next()))?
    else
      None
  }

但是,如果我在{中使用(代替Future,则代码会编译。

def updateUser(newValues:User):Future[Option[User]] = {
    println("updating user: "+newValues)
    Future {
      println("updating user: "+newValues)//using { in place of ( works
      updateOneById(newValues, UserKeys(1, newValues.profile.externalProfileDetails.email))
    }}

1 个答案:

答案 0 :(得分:3)

如果使用括号,则编译器期望括号内有一个表达式,而不是代码块。添加了println(...)后,编译器将通过在updateOneById语句的返回值上调用println方法来使括号的内容成为一个表达式。但这是Unit类型,没有该方法。

如果用花括号替换括号,则编译器需要一个代码块,并且可以将2行分成2条语句。 如果在println后面加上分号来向编译器指示这是2条语句,则可能会得到一条更清晰的错误消息。