Hacker News new | past | comments | ask | show | jobs | submit login
Concurrency: Implement withUnsafe[Throwing]Continuation (github.com/apple)
65 points by adamnemecek on Dec 2, 2020 | hide | past | favorite | 15 comments



This isn’t “real” call/cc; the “continuation” here must be suspended and then resumed exactly once. It’s intended for writing async wrappers for callback-based APIs that take a completion function.


Ok, we've changed the title from "Swift to Add call/cc".


I saw the term "one-shot continuation" used to refer to this.


Yeah. This change doesn’t really introduce a new capability per se; it just ties together some of the existing concurrency primitives that other folks are working on in a new way.


As a low-level primitive to be used with await, only compatible with async functions.

Which, if you think about it, is not as unusual as it sounds! In JavaScript, for instance, awaiting an object is equivalent to calling .then() on it, passing a closure which, when called, continues the function that awaited. In other words, JavaScript’s await is call/cc. It just uses .then() instead of a direct call, and has some extra complexity in the form of the ‘rejected’ callback. Other than that it’s the same thing.

Edit: Well, except for the part where you can’t call the resume function more than once (I think?). Which is apparently also the case with this Swift version.


PR written by Slava Pestov, the creator of Factor: https://factorcode.org


i wish more languages provided call-cc. some languages let you freeze/restore a single stack frame, but it's unfashionable these days to freeze/restore an entire call chain. i find it to be a clean building block for all sorts of operations, like undo, multithreading, exception handling, etc. i missed this feature so much in python that i added it as a library [0].

[0] https://github.com/a-rahimi/python-checkpointing2


Wouldn't it be safer to have a purpose-built state save and restore? This is a cool trick and will work in tightly controlled situations, but I wouldn't be comfortable using it with any third party library (so any non-trivial app) or if there's a chance of version update which changes state structure.


I would prefer more languages provided delimited continuations, rather than “full” continuations like call/cc.

See this paper by Oleg Okmij: http://okmij.org/ftp/continuations/against-callcc.html


I would prefer more languages provided copyable coroutines, because I would rather explain create+switch+copy to people than call/cc or shift+reset


Why not use greenlet? does it lack serializability?


greenlet good! i'll check if it saves & restores the entire callchain or if it only works on the current frame like generators and async already do.


It's real coroutines. I used it in production in 2010 before a worse thing made it into the language and in 2019 after.


How does this compare to Kotlin coroutines/continuations ?


Here is an overview of the planned concurrency features: https://forums.swift.org/t/swift-concurrency-roadmap/41611




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

Search: