slick 运行 中的 if-else 表达式,更新方法 scala 函数

If-else expression in slick run, update method scala function

我想用 if-else 语句检查变量是否 newPsInfo.clearedCanLoadSC

是真的那么我想制作一个其他日期的今天的时间戳 所以我尝试了

ternary if-else with 
condition? true : false

newPsInfo.clearedCanLoadSc.equals(true) ? 
LocalDate.now() : LocalDate.of(2000,1,1)

但不幸的是不起作用

首先,我通过 _.id 进行过滤,然后通过 productSettingsTable class 将结果映射到新的 productSettingsInfo 参数的新更新值。 所以我的问题是我可以将 if - else 语句插入到 .map 或 .update 方法中,如下所示:

    newPsInfo.clearedCanLoadSc.equals(true) ? 
    LocalDate.now() : LocalDate.of(2000,1,1))
def update(employer: Employer, newPsInfo: PsInfo): Future[Int] =

    db.run(
      productSettingsQuery.filter(_.employerId === employer.id).map(productSettings =>
        (productSettings.enableSc, productSettings.enableConversion,
          productSettings.enableRefundDays, productSettings.enableOutOfPocketPayment,
          productSettings.clearedCanLoadSc, productSettings.enableL, productSettings.clearedAt)).
     update((newPsInfo.enableSc, newPsInfo.enableConversion,
          newPsInfo.enableRefundDays, newPsInfo.enableOutOfPocketPayment,
          newPsInfo.clearedCanLoadSc, newPsInfo.enableL,newPsInfo.clearedCanLoadSc.equals(true) ? LocalDate.now() : LocalDate.of(2000,1,1)))
    )

问题是我的 if else 子句不起作用 Intelli j 显示错误无法解析符号?

那么有没有办法将 if-else 语句插入到 .map 或 .update 函数中?

Scala 没有三元条件运算符。而是简单地使用 if-else 这样的表达式

if (newPsInfo.clearedCanLoadSc) LocalDate.now() else LocalDate.of(2000,1,1)

请注意 if-expression 确实是一个 表达式 计算结果为一个值,而不是一个控制结构,例如

val x: String = if (true) "foo" else "bar"
x // res0: String = foo

针对注释,控制结构是诸如 while 循环或 if-then 条件语句之类的构造,它们的目的是根据某些程序状态改变程序控制流。现在,Scala 显然有这些,但我们说它们是 expressions because not only do they change the execution flow, they also evaluate to a value。将此与 Java 的 if 语句进行对比:

String x = if (true) "foo" else "bar";

导致错误

error: illegal start of expression
String x = if (true) "foo" else "bar";

请注意我们如何无法评估它并将其分配给变量 x