Skip to content

purerl bindings to basic Erlang process functionality

License

Notifications You must be signed in to change notification settings

id3as/purescript-erl-process

 
 

Repository files navigation

purescript-erl-process

Bindings to Erlang processes (spawn, send and receive).

Type-safe bindings

Bindings with an attempt to enforce a layer of type-safety are given in Erl.Process (this assumes both sender and receiver are constructed via this mechanism).

A process is given some type Process a, and only values of type a can be sent to it. All process spawning, message sending and receiving takes place in the Effect monad.

Firstly we can define a receiving function:

logger :: forall a. (Show a) => Effect a -> Effect Unit
logger receive = do
  a <- receive
  log $ "Received: " <> show a <> "\n"

And then we can launch the process with spawn and send messages to it with (!):

main :: Effect Unit
main = do
  p <- spawn logger
  p ! 42

In practice the a in Process a is likely to be some ADT representing the various possible messages that may be sent to the process.

More examples can be found in the tests.

Raw (unsafe) bindings

Low level (unsafe) FFI bindings are provided in Erl.Process.Raw.

proc :: forall eff. Efffect Unit
proc = do
  n :: Int <- receive
  log $ "Received: " <> show n
  proc
 
main = do
  p <- spawn proc
  p ! 42

Here the receive could be given any type annotation, and if a different type of value is sent, the process will crash.

This corresponds to the Erlang code

proc() ->
  receive
    X -> io:format("~p~n", [X])
  end.
  
main() ->
  Pid <- spawn(fun proc/1),
  Pid ! 42.

About

purerl bindings to basic Erlang process functionality

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PureScript 80.9%
  • Erlang 14.1%
  • Dhall 3.7%
  • Other 1.3%