I want to check with if-else statement if the variable newPsInfo.clearedCanLoadSC
is true then i want to make a Timestamp of Today else of some other Date so i tried the
ternary if-else with
condition? true : false
newPsInfo.clearedCanLoadSc.equals(true) ?
LocalDate.now() : LocalDate.of(2000,1,1)
but unfortunately doesn't work
First I .filter by _.id then I .map the results by productSettingsTable class to the new updated values of new productSettingsInfo parameter. So my question is can i insert an if - else statement into the .map or .update methods like this:
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)))
)
The problem is that my if else clause is not working the Intelli j shows errors Cannot resolve symbols ?
So is there a way to insert an if-else-statement into the .map or .update function?
Scala does not have a terinary conditional operator. Instead simply use if-else expression like so
if (newPsInfo.clearedCanLoadSc) LocalDate.now() else LocalDate.of(2000,1,1)
Note that if-expression is indeed an expression that evaluates to a value, not a control structure, for example
val x: String = if (true) "foo" else "bar"
x // res0: String = foo
Addressing the comment, control structures are constructs such as while loop, or if-then conditionals, and their purpose is to alter the flow of program control on the basis of some program state. Now, Scala obviously has those, but we say they are expressions because not only do they change the execution flow, they also evaluate to a value. Contrast this with Java's if statement:
String x = if (true) "foo" else "bar";
which results in error
error: illegal start of expression
String x = if (true) "foo" else "bar";
Note how we are unable to evaluate it and assign it to variable x
.