functional programming - Scala compiler not executing the values which are not returned -
i new scala. writing code , found unusual behaviour scala. had written code
for { verification <- verifyreset(hash, timestamp, id) resp = if (verification) { setpassword(id, password) setactive(id) httpresponse(accepted, seq(location(empty withpath path / id))) } else httpresponse(unauthorized) } yield resp here setpassword , setactive used db operations. while testing found loop yielded resp(accepted) without executing setpassword , setactive. hypothesized since compiler doesn't need execute functions in-order return last statement, didn't execute them.
but then, wrote similar function behaved pretty different hypothesis,
r = if(true){ println("1st statement") println("2nd statement") 2*3 } i got r 6 (normal behavior), gave me output on console
1st statement 2nd statement
i didn't understand because in calculating 2*3 don't need execute println, why println executed?
your hypothesis wrong here.
it execute operations before returning last statement. the last statement returned default. there must other error
setpassword(id, password) setactive(id)
Comments
Post a Comment