awkward-squad / ki

A structured concurrency library

Home Page:https://hackage.haskell.org/package/ki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Consider removing `Thread`

mitchellwrosen opened this issue · comments

Currently, the thread API is just:

fork :: Scope -> IO a -> IO (Thread a)
await :: Thread a -> IO a
awaitSTM :: Thread a -> STM a
awaitFor :: Thread a -> Duration -> IO (Maybe a)

Since you can't cancel a single thread (should you be able to...?), we could instead just represent a thread as the STM a that awaits its return value.

fork :: Scope -> IO a -> IO (STM a)

await and awaitFor could still be exported, albeit with kind of weird definitions that don't exactly relate to any concepts in ki specifically.

await :: STM a -> IO a
await = atomically

awaitFor :: STM a -> Duration -> IO (Maybe a)
awaitFor x s = timeoutSTM s (pure . Just <$> x) (pure Nothing)

The benefit here would be removing one noun from the interface. Not exactly sure if that's desirable in this case :)

I think this probably isn't a good idea for a few reasons. One is that the IO a or STM a action that represents awaiting on a running thread is only valid within the scope that thread was spawned in, and having a type called Thread is a nice reminder of that.