Why the color of a function is our friend

When talking about 'stackful coroutine vs stackless coroutine', Some people claims that stackless coroutine's async color is infectious. Once a function awaits for async operations, it's async too. Therefore stackful coroutine is better for managing complex coding.

This point is TOTALLY WRONG.

All colors, not only async, are all our friends! They can help us manage complex structures of program.

Stackful goroutine without color is not that good

Suppose a Golang function with the following signature:

func handle()

and now a new programmer Ireina joins the project and simply use the function in a blocking function logic:

func logic() {
    handle()
}

If function handle is a non-blocking call, then everything is ok. However, if handle is a blocking call or can consume much time and Ireina didn't notice this, logic may block the whole program. Even if logic cause no serious problem, another new programmer may create another function logic2 calling logic, and logic3 calling logic2, which wil eventually cause blocking problem.

Functions without color still need better document or comment to tell the caller how to use it and how to choose proper context to use it. Even worse, if some inner function may panic, it will destroy the whole calling chain!

How about channels as return type?

func handle() <-chan string

This way is much much better! but in fact it is marking async effect now! This is not about stackful coroutines, it's just about abstraction and constraints.

Async should be marked and controled

Now suppose handle is marked as async in Rust:

#![allow(unused)]
fn main() {
async fn handle();

// or
fn handle() -> impl Future<Output=()> + Send;
}

Now Ireina only has 2 options of using it:

  • Explicitly use a runtime to block on it until finished.
  • Mark logic as async too.

She can never simply blocking on it without noticing.

Colors can serve as a tool which indicate many side-effects a function can cause. Also, colors within a proper type system can prevent many potential bugs and performance issue.

Color of effects

Beside async, there also exists many kinds of effects, such as error handling, IO, optional, dependency injection, etc. We can even compose these effects or even abstract over it to support various kinds of effects without changing code!

Embrace colors and effects!