Hacker Timesnew | past | comments | ask | show | jobs | submitlogin

After reading your post I struggled to decide whether I truly believed that coroutines were better than callbacks. This HN thread provided some useful discussion for me: https://qht.co/item?id=1549168

I still don't know enough to take sides, but the thread I just linked helped me to better understand the issue.



By coroutine, he really means a language feature that converts:

    do_something_and_then(function (){
        do_another_thing_and_then( function (){
            say "we're done!"
        });
    });
to:

    do_something;
    do_another_thing;
    say "we're done";
Coroutines are one way of doing this, but source rewriting (Haskell-"do-notation"-style) also works.

An instructive example is the Coro module for Perl (which is a bit more coroutine-ish, as it provides separate perl and C stacks for each async action, etc.): http://search.cpan.org/perldoc?Coro

It lets you write something like:

    async {
        my $t = AnyEvent->timer( after => 5, cb => Coro::rouse_cb );
       Coro::rouse_wait();
       say "OH HAI";
    };
instead of:

    my $t = AnyEvent->timer( after => 5, cb => sub {
       say "OH HAI";
    });
  
This may look like it's not an improvement, but it is after you add some sugar:

    sub sleep($) {
        my $seconds = shift;
        my $cb = Coro::rouse_cb;
        my $t = AnyEvent->timer( after => $seconds, cb => $cb );
    }

    async {
        sleep 5;
        say "It's been 5 seconds!";
        sleep 10;
        say "It's been 15 seconds!";
    };
instead of:

    my $t; $t = AnyEvent->timer( after => 5, cb => sub {
        say "5";
        $t = AnyEvent->timer( after => 10, cb => sub {
            say "15";
            undef $t;
        });
    });


I don't think one really needs to take sides one simply needs to know their tools and know that they can be dangerous if not used properly. Both sides of the coin have merits. Personal I am a big Haskell user so my views on IO should probably be disregarded.


On the contrary, as a Haskell user your views on IO should be kept in a thunk waiting for evaluation if they're ever needed. Unless you're one of those guys who've figured out how to do iteratee IO, in which case I tip my hat to you.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: