# Quiet for comprehensions

**URL:** <https://contributors.scala-lang.org/t/quiet-for-comprehensions/6160>\
**Category:** Language Design\
**Created:** [April 18, 2023, 4:39pm UTC](https://contributors.scala-lang.org/t/quiet-for-comprehensions/6160 "2023-04-18T16:39:38Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![sideeffffect](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sideeffffect/32/1466_2.png) [@sideeffffect](https://contributors.scala-lang.org/u/sideeffffect)\
**Post date:** [April 18, 2023, 8:36pm UTC](https://contributors.scala-lang.org/t/quiet-for-comprehensions/6160/8 "2023-04-18T20:36:04Z")

</div>

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

> **[Computation Expressions - F#](https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions)**
>
> Learn how to create convenient syntax for writing computations in F# that can be sequenced and combined using control flow constructs and bindings.

For Scala the code then could look something like this

```scala
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:

> [@From freeing \\leftarrow. to better for and unification of direct and monadic style](https://contributors.scala-lang.org/t/from-freeing-leftarrow-to-better-for-and-unification-of-direct-and-monadic-style/5922/2):
>
> I feel like that we need to improve the for-comprehension itself. It should be generalized to match the other syntactic/control flow constructs in Scala. The regular Scala constructs, like val, if, throw would have their counterparts with !, like val! (currently \<-, flatMap), if! (ifM), throw! (raiseError), etc… The beauty of this approach is that it builds on top of the already existing concept of for-comprehensions and uses similarly named keywords for analogous things (there is just ! appen…

> [@Scala 3 very impressive, It can replace Python i think](https://contributors.scala-lang.org/t/scala-3-very-impressive-it-can-replace-python-i-think/4768/14):
>
> F# is really nice and simple language, not as powerful as Scala, but still very well designed. Two things that immediately come to mind that F# does better than Scala: I have to start with syntax. Syntax is the least important aspect of a language, but F# is just so nice. It’s simple, regular, and very light. It has the most crisp syntax I have ever seen, better than Python, Haskell, Scala or whatever else. Scala (and all other languages) should learn from it: [https://fsharpforfunandprofit.com…](https://fsharpforfunandprofit.com/posts/fsharp-in-60-seconds/)

> [@Design of -Xasync](https://contributors.scala-lang.org/t/design-of-xasync/4419/15):
>
> I think that whenever we discuss async/await-like programming or for-comprehension in Scala, we should take a step back and have a look at [F#'s Computation Expressions](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions). They are a variant of Haskell’s do-notation or of Scala’s for-comprehension, but it’s much more generalized and customizable. It doesn’t work only with flatMap \<- (as if “monadic” val x = ...) and pure yield, but also works with for/while loops, sequences, pattern matches and resource disposal and try-catch-finally analogs from …

> [@Pre SIP: \`for\` with Control Flow (an alternative to Scala Async, Scala Continuations and Scala Virtualized)](https://contributors.scala-lang.org/t/pre-sip-for-with-control-flow-an-alternative-to-scala-async-scala-continuations-and-scala-virtualized/4423/10):
>
> Interesting take! But I think we don’t really need to copy F# verbatim here. F# does certain things the way it does, because it lacks other features that Scala possesses even today. Needing to have an explicit “builder” object is one such example. In Scala, we have implicit parameters, extension methods and higher-kinded types. We should do fine with only those. What we would need, is a new do construct. Let me illustrate it with this short snippet: def fetchAndDownload(url: Url): IO[Data] = …

> [@Suspendable functions and coroutines](https://contributors.scala-lang.org/t/suspendable-functions-and-coroutines/3772/5):
>
> I think that call more for improving the for-comprehensions, to [something like what F# has](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions)

> [@Pre-SIP: improve for-comprehensions functionality](https://contributors.scala-lang.org/t/pre-sip-improve-for-comprehensions-functionality/3509/9):
>
> +1 for embracing fully [F#-like computation expressions](https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions) But the problem with the current for comprehension and redundant map could be solved by a relatively small change: What we need is a for comprehension without the yield part. So instead of for { a \<- f1 b \<- f2(a) c \<- f3(a, b) } yield c it would be just for { a \<- f1 b \<- f2(a) f3(a, b) } That’s how Haskell does it, btw. Allowing that seem easily doable, doesn’t it? Or are there problems with it?

On Reddit:  
[https://old.reddit.com/r/scala/comments/y6zyx9/the\_case\_against\_effect\_systems\_eg\_the\_io\_data/#ist9r0f](https://old.reddit.com/r/scala/comments/y6zyx9/the_case_against_effect_systems_eg_the_io_data/#ist9r0f)

---

_[View the full topic](https://contributors.scala-lang.org/t/quiet-for-comprehensions/6160)._
