Why Don't More Languages Have a call/cc Operator?
30 Oct 2023
Something I’ve wondered about for a little while: why don’t more languages have a call/cc
operator? Having first-class continuations in your programming language gives your programmers a powerful construct. So why do only a handful of languages have it?
The short answer is: it’s tricky to implement efficiently. One way to get call/cc
is to convert your code into continuation-passing style. Then, call/cc
simply takes the continuation in that representation and binds it to a variable. Most languages don’t seem to go through a continuation-passing style conversion pass though, so there’s no continuation to grab.
I asked Matthew Flatt about this today, and his answer was that most languages use the C model of functions: when you call a function, you push the arguments to the function onto a stack along with the return address. Then, when you return, you pop those element back off the stack. To get call/cc
, you’ve have to copy the entire stack and pass that around.