3-message-passing
Now that we've learned to spawn processes, we can start sending messages to them.
Every message in Riot is typed. And all messages form part of the Message.t
type. To define a new message, you can write:
type Message.t += Hello_world
Your message can have any shape you want, so long as it fits into this message
type. Once a message is defined, we can start a process that knows how to
receive them. To receive a message we use the receive_any function, like this:
match receive_any () with
| Hello_world -> print_endline "Hello, World! :D"
receive_any () will try to get a message from the current process mailbox. If
the mailbox is empty, receive_any () will suspend the process until a message
is delivered.
Since messages are represented with an open variant, when we pattern match on
receive_any () we will have to make sure to handle or ignore other messages.
match receive_any () with
| Hello_world -> print_endline "Hello, World! :D"
| _ -> print_endline "Oh no, an unhandled message! D:"
Within a process, it is okay for us to do a partial match, since a process crashing isn't going to take the runtime down. So an alternative way to write this is:
match[@warning "-8"] receive () with
| Hello_world -> print_endline "Hello, World! :D"
Although I would recommend you to be careful where you disable this warning, since exhaustive pattern-matching is one of OCaml's best features.
Next Steps
- the next step introduces you to long lived processes