Hi,

We all love the ability to `use GenServer` in order to provide default
stubs for callback functions, saving us time and effort of writing the same
boilerplate over and over again.

However, I'm sure many of you have also been bitten by this very feature
where you made a typo in the signature of init/1 or handle_info/2 and was
left scratching your head wondering why the gen server's state looks so
weird or whether it receives any messages sent to it.

I'd like to propose an addition that will make the default stubs stricter
in terms of handling unexpected input. The idea is basically adding the
`strict` option to the `use GenServer` call. When that option is supplied
with value `true`, the following changes will take effect:

  1. There will be no default implementation of init/1. If the user forgets
to implement it or implements it incorrectly by supplying a different
number of arguments, the compiler will warn about the missing init/1
implementation.

  2. The default implementation of handle_info/1 will exit on any incoming
message, in the same way handle_cast/2 and handle_call/3 already do.

The change will be backwards-compatible if we make `strict: false` the
default. However, we should mention in the docs that calling `use
GenServer, strict: true` is the recommended way for any serious purpose and
that `use GenServer` should be reserved for playground or throw-away code.

Here's a proof of concept –
https://github.com/elixir-lang/elixir/compare/master...alco:alco/strict-gen-server

Best regards,
Alex

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/CAAPY6eOSggAn6T3k3E0crKx1H381XUdrccw7KTZhqA9iod6jhA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to