|
First of all, as of January, Elixir has gotten its own type-system[0][1]. And, even before, one could annotate Erlang and Elixir with typespecs and then run a tool called dialyzer over the code for type-checking[2]. (And let's not forget that, even without using actual annotations, BEAM languages have pattern-matching, which can give you a kind-of poor-man's typing, if you will). Second, the point of "let it crash" is not that you can then login to the running VM, but rather that your GenServers, etc. would be part of a "supervision tree", i.e. your processes would be monitored by a Supervisor which would have a "restart strategy" that you wrote to deal with crashed processes. So, you can write code using the "happy path" in cases where you expect things to usually work and, if there's an error, you can let the process crash because it'll get restarted, in a matter of microseconds, by its Supervisor. It's almost a cliche to hear stories about people running Erlang or Elixir who didn't realize they had a bug in their system for months until they happened to look at the logs and see notifications of a supervised-process getting restarted often. (And, btw, in cases where you might expect to have lots of potential errors, e.g. from getting data from an external API, you can use exception). [0] https://elixirforum.com/t/jose-valim-elixir-is-officially-a-... [1] https://www.irif.fr/_media/users/gduboc/elixir-types.pdf [2] https://fly.io/phoenix-files/adding-dialyzer-without-the-pai... |
This means you don't have to code defensively: if your code crashes, it's ok.
This is important because in erlang/elixir you have pattern matching, which allows you to code declaratively. Example:
Imagine you have a function that expects a two-element list, `[a, b]` and then adds them.
In elixir, you could write the function this way:
``` def add([a,b]) do a+b end ```
Note that the program will crash if you call: `add([1,2,3])` or `add([1])`.
In other words, you can write code such that the shape of the code matches the shape of the data.
If the data looks any other way than the expected one, it will crash. But this is a good thing! It's better than operating on data that has the wrong shape, and thhe OTP architecture should restart the process intelligently.