Quiet for comprehensions

It would be awesome if Scala improved upon the current state of for-comprehensions.
We don’t have to look far for inspiration, we can do something similar to what F# is doing with so called Computation expressions

For Scala the code then could look something like this

def userLogin(username: String, password: String): Future[UserInfo] = for
  val dbReq = CheckUserLoginRequest(username, password)
  val! userId = userDb.login(dbReq)
  val! profile = profileDb.fetch(userId)
  val! avatar = avatarDb.fetch(userId)
  yield UserInfo(profile, avatar)

A general de-sugaring table could be like this

ordinary code analogue for-comprehension sugar for
val ... = ... val! ... = ... (legacy ... <- ...) flatMap
if ... then ... else ... if! ... then ... else ... ifM
if ... then ... if! ... then ... whenM
throw ... throw! ... raiseError
try ... catch ... try! ... catch ... handleErrorWith
while ... do ... while! ... do ... whileM
use ... = ... (hypothetically) use! ... = ... use

The point here is to

  • enable as many analogues to syntactic constructs from ordinary code (val, try, if, …) as possible
  • make them look as similar to ordinary code constructs as possible. Appending ! is one possible way to do that. val! is better analogue to val than <- can ever be.

Previously discussed on this forum:

On Reddit:
https://old.reddit.com/r/scala/comments/y6zyx9/the_case_against_effect_systems_eg_the_io_data/#ist9r0f

5 Likes