简体   繁体   English

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

[英]why adding a multi-line comment in the code makes it uncompilable

In the following function, if I add a multi-line comment then the code doesn't compile. 在以下函数中,如果我添加多行注释,则代码不会编译。 Why? 为什么?

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

  }

But

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

      }

Fast check in ammonite: 快速检查菊石:

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)

shows that parser interpret them as 表明解析器将它们解释为

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)

Why? 为什么? Why isn't it simply removing comments and acting as if they weren't there? 为什么不简单地删除评论并表现得好像他们不在那里?

My guess is that it is a bug in compiler, coming from the fact that Scala's parser tries to track all that created the AST - empty characters and comments including - and when it analyses whether it should be: 我的猜测是,它是编译器中的一个错误,来自Scala的解析器试图跟踪所有创建AST的事实 - 空字符和注释包括 - 当它分析它是否应该是:

method(arg1, arg2)

or 要么

value
(next expression in parenthesis)

it compares line numbers to distinguish them. 它比较行号以区分它们。 Apparently no one considered the corner case when such handling of multi line comment would change the semantics. 显然,没有人会考虑角线情况,因为这种多行注释的处理会改变语义。 (Notice than in other cases this doesn't product erroneous behavior, which is why probably no one bothered with fixing this). (注意,与其他情况相比,这不会产生错误的行为,这就是为什么可能没有人为解决这个问题而烦恼)。

Update: you might try to fix this by forcing compiler to acknowledge it as one expression eg by moving left parenthesis before the comment: 更新:您可以尝试通过强制编译器将其确认为一个表达式来解决此问题,例如通过在注释之前移动左括号:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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