Direct style (Rust) instead of for comprehensions

In dotty-cps-async (where you use ‘just scala’) exists a short syntax for await as unary_!. enabled by import syntax, so now it’s possible to write:

import cps.*
import cps.syntax.*


def dbFun() = async[YouEffect] {
   val  db =   ! Database.open()
   while( await(db.hasNextRow()) ) {
         if ( await(db.lockNextRow() ) {
             val nextRow = ! db.nextRow()
             doSomethingWith(nextRow)
         }else{
             whaitT()
         }
   }
}

Here I leave await in places, where ! can be read as boolean expression.
It’s still verbose, from my point of view.
To go further, we can next alternatives:

  • Automatic coloring
import cps.*
import cps.automaticColoring.given
import scala.language.implicitConversions


def  dbFun() = async[YouEffect] {
   val  db =   Database.open()
   while( db.hasNextRow() ) {
         if ( db.lockNextRow() ) {
             val nextRow =  db.nextRow()
             doSomethingWith(nextRow)
         }else{
             whaitT()
         }
   }
}

(but it’s too hight-level for many cases because developers now need to look at function definitions to restore typing in mind)

Then this code can be written as:

import cps.*


def dbFun() = async[YouEffect] {
   val  db  <-  Database.open()
   while( await(db.hasNextRow()) ) {
         if ( await(db.lockNextRow() ) {
             val nextRow <-  db.nextRow()
             doSomethingWith(nextRow)
         }else{
             whaitT()
         }
   }
}

1 Like