简体   繁体   中英

Why adding a println makes my code uncompilable

In this function

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))
  )}

If I uncomment the above code, I get error cannot resolve updateOneById

The above method is defined in the following class

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

and updateOneById is defined in CassandraRepository as

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
  }

However, if I use { in place of ( in the Future then the code compiles.

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))
    }}

If you use parentheses the compiler expects an expression inside the parantheses and not a block of code. With the println(...) added the compiler tries to make the contents of the parantheses into one expression by invoking the updateOneById method on the return value of the println statement. But this is of type Unit and doesn't have that method.

If you replace the parentheses by curly braces the compiler expects a block of code and can separate the 2 lines into 2 statements. You might get an error message that is a bit clearer if you put a semicolon after the println to indicate to the compiler that these are 2 statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM