-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Better typing for trapped exits and monitor messages #20
Comments
Thanks for sharing! This is very cool :) This is the same design that the gleam-experiments/otp_process library currently uses: https://github.com/gleam-experiments/otp_process/blob/02f8ba0b5b3c3c39096ad7117f9a486e6c5281eb/src/gleam/otp/process.gleam#L141
What is the problem with calling it twice? It could potentially change the mapping function, but I don't see a problem with that? Perhaps I'm missing something.
This is not type safe as the msg transformer function could return a type different to that which the process accepts as a message.
Does this mean the process will hold a map of functions that take pids/ports and return a message? I think we'd need to remove the function from the map once the that down message has been received to prevent a memory leak. |
Maybe it isn't a problem, although I can't see a reason to change the mapping function.
You can type the spawn function to make sure they have to be the same return type. fn myfunc(foo: fn() -> a, bar: fn() -> a) {
todo
}
fn demo() {
myfunc(fn() { 2 }, fn() { 2.0 })
}
Basically yes, although I would have the key of the map be the reference from the monitor, it's possible to have more than one monitor point to the same pid |
I think there is an option 4. process.spawn_link(fn(receive) {
// continue ...
}, [TrapExit(fn(pid, reason) { Exit(pid, reason) })]) Then if you are not trapping exits you can just put an empty list. process.spawn_link(fn(receive) {
// continue ...
}, []) This is much neater than the Error(Nil) as last argument. p.s. this is my new favourite approach |
That is 100% what the my OTP process module does 😁 (Or did, I'm currently fiddling with it) |
Looks like it was a while back, but still! I like this design. |
See this Elixir forum post for my explination of typed processes up to this point.
Background
Exits.
A process traps exits by providing the function that will handle the message.
Start with a process with the following message type
The Exit type is
OhDear
to make clear its a type defined by the client, it would I expect normally be called exit in practice.Option 1
Signature of trap_exit function.
Option 2
Option 3
Monitors
Very similar except the mapping function is passed when the monitor is created.
The text was updated successfully, but these errors were encountered: