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))
}
解决方案
快速签入菊石:
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)
推荐阅读
- python - Python根据条件将值转移到列
- javascript - 在对象中添加新道具时尝试使用过滤器
- typescript - 使用momentjs将日期从阿拉伯语格式化为英语
- powerbi - 在 PowerBI 仪表板中复制视觉对象
- javascript - 如何在js中使用键盘事件创建计数计时器
- arrays - C ++扫描书面文本崩溃程序?
- azure - 如何检查 Azure 专用 SQL 池中的 MAXDOP 配置
- python - 如何在 Django 中播放列表的多对多字段的模板中显示 YouTube 视频数据,例如视频名称和视频 url
- laravel - Laravel“PHP Artisan route:list”失败,目标类 [App\Http\Controllers\API\Auth\LoginController] 不存在
- visual-studio-code - 有没有办法获取其他 VSCode 窗口/实例工作区文件夹?