为什么在代码中添加多行注释使其无法编译

时间:2018-09-03 07:08:45

标签: scala

在以下函数中,如果我添加多行注释,则代码将无法编译。为什么?

def updateValues(tableName:String, model:User, id:UserKeys):Update.Where = {
    QueryBuilder.update(tableName).`with`/*WORKS*/(QueryBuilder.set("confirmed",model.profile.internalProfileDetails.get.confirmed))/*(QueryBuilder.set("authprovider",model.profile.internalProfileDetails.get.loginInfo.providerID)) //TODOM - remove hardcoding for bucket and also add bucket in User model*/
      .and(QueryBuilder.set("id",model.id))
      .and(QueryBuilder.set("password",model.profile.internalProfileDetails.get.passwordInfo.get.password))
      .and(QueryBuilder.set("hasher",model.profile.internalProfileDetails.get.passwordInfo.get.hasher))
      .and(QueryBuilder.set("salt",""/*model.profile.internalProfileDetails.get.passwordInfo.get.salt.get*/)) //salt is empty for BCryptSha256PasswordHasher. The 'hash' method of BCryptSha256PasswordHasher does not return the salt separately because it is embedded in the hashed password.
      .where(QueryBuilder.eq("bucket", id.bucket)) //TODOM - pick column names from config/env file
      .and(QueryBuilder.eq("email", id.email))
      .and(QueryBuilder.eq("authprovider", model.profile.internalProfileDetails.get.loginInfo.providerID))//TODOM - this should come from id
      .and(QueryBuilder.eq("firstname",model.profile.externalProfileDetails.firstName))
      .and(QueryBuilder.eq("lastname",model.profile.externalProfileDetails.lastName))

  }

但是

    def updateValues(tableName:String, model:User, id:UserKeys):Update.Where = {
        QueryBuilder.update(tableName).`with`/*
DOESNT WORKS. I get error, cannot resolve and */(QueryBuilder.set("confirmed",model.profile.internalProfileDetails.get.confirmed))/*(QueryBuilder.set("authprovider",model.profile.internalProfileDetails.get.loginInfo.providerID)) //TODOM - remove hardcoding for bucket and also add bucket in User model*/
          .and(QueryBuilder.set("id",model.id))
          .and(QueryBuilder.set("password",model.profile.internalProfileDetails.get.passwordInfo.get.password))
          .and(QueryBuilder.set("hasher",model.profile.internalProfileDetails.get.passwordInfo.get.hasher))
          .and(QueryBuilder.set("salt",""/*model.profile.internalProfileDetails.get.passwordInfo.get.salt.get*/)) //salt is empty for BCryptSha256PasswordHasher. The 'hash' method of BCryptSha256PasswordHasher does not return the salt separately because it is embedded in the hashed password.
          .where(QueryBuilder.eq("bucket", id.bucket)) //TODOM - pick column names from config/env file
          .and(QueryBuilder.eq("email", id.email))
          .and(QueryBuilder.eq("authprovider", model.profile.internalProfileDetails.get.loginInfo.providerID))//TODOM - this should come from id
          .and(QueryBuilder.eq("firstname",model.profile.externalProfileDetails.firstName))
          .and(QueryBuilder.eq("lastname",model.profile.externalProfileDetails.lastName))

      }

1 个答案:

答案 0 :(得分:4)

快速检查炸药:

val x = List/*
multi */(1, 2, 3) 
x: List.type = scala.collection.immutable.List$@3697a307

val x = List/* single */(1, 2, 3)
x: List[Int] = List(1, 2, 3)

表明解析器将其解释为

val x = List
(1, 2, 3) 
x: List.type = scala.collection.immutable.List$@3697a307

val x = List(1, 2, 3)
x: List[Int] = List(1, 2, 3)

为什么?为什么不简单地删除评论并表现出好像不在的样子?

我的猜测是这是编译器中的一个错误,这是由于Scala的解析器试图跟踪所有创建AST的事实-空字符和注释,包括-以及何时分析是否应这样:

method(arg1, arg2)

value
(next expression in parenthesis)

它比较行号以区分它们。显然,没有人考虑过这样的多行注释处理会改变语义的情况。 (注意,在其他情况下,这不会产生错误的行为,这就是为什么没人可能会为解决此问题而烦恼的原因。)

更新:您可以通过强制编译器将其确认为一个表达式来尝试解决此问题,例如通过在评论前移左括号:

val x = List(/*
multi */1, 2, 3)
x: List[Int] = List(1, 2, 3)