by _v7gu · 6y ago · 0 comments
So what are you unable to accomplish in Erlang that makes you feel like it’s missing something?
You can write extremely reliable code in C (e.g. medical devices and car ECUs) but you have to be vary careful with any concurrency and usually dynamic memory allocation is banned. An enormous amount of testing and other design rules are needed. Maybe Rust would help to automatically check some of those things that otherwise require a lot of work to achieve.
You can write highly concurrent server applications in C. It's a lot of work and bug prone. If you write them in Elrnag or Elixir your life will be much easier. That's why we use them.
Maybe a static type system would help enforce some invariants about your code. Maybe it saves you from witting some classes of test. Basically, maybe you get to the same endpoint but with less work.
The things that help you usually also limit you
See: training wheels or armbands
I've seen much code written in static typed languages (mostly JVM languages) that supposedly help the programmer to write better code or code with less errors just by playing tennis with the compiler
Thruth is that most of the code I've seen written "with type aids" is terrible from any other point of view
The types are correctly passed to each function, but what purpose those types serve it's a mistery
When you only have numbers, binaries, lists and tuples,you are forced to think about data structures in a simpler and more elegant way
IDE generated "type safe" code too often is simply "a class for everything" and bad abstractions
Most of the time when I review code my remarks is: it should be a 10 lines function, why did you write 10 classes with 10 getters and setters each?
Java is an extremely poor example of a helpful type system. Consider OCaml, Haskell, Elm, and others which avoid much of the verbosity you describe.
Static typing is really helpful when doing refactoring. The fact that a statically typed program compiles gives you important assurances - especially post-refactor. It's not a silver bullet but it's also certainly not "playing tennis with the compiler" as you describe.
Static typing with functional programming languages works really well because the types are so simple. If you look at OCaml, or Haskell, or F# you simple types, but with a lot of benefit of type checking.
This is the fault of the language or programmer itself instead of its type system.
I am programming in Dart and sometimes I feel that I am writing some code just to workaround some limitations in the language (the language itself is nice 90% of time, just to make it clear).
Now, I also program in Python using mypy, and I never needed to write "10 getters and setters" to make the compiler happy. Sometimes I do need to write additional code to make mypy happy, but generally it is in overly clever code that mypy doesn't understand. So generally fixing the issues makes the code better.
and yet, none of us seem to be programming in binary.... huh. Maybe there's some value in those "training wheels".
Examples:
- I accidentally pass in 3 params instead of 4 to a function (with static typing this does not compile)
- I forgot to handle an edge case (I can use a discriminated unions for example to handle all cases explicitly)
- I did not check if something was null and pass it in to a function (does not happen in most ML languages)
I know that you can have editor plugins to handle these, still. I can develop reliable code much faster in F# than in Clojure, even though I love Clojure to death.
You can do many things in Erlang that helps with these, still it would be really good to have a language that unifies Erlang and ML (any ML with Hindley–Milner type system would work).
In Erlang this will also not compile, unless you have a 3 parameter version of the function, in which case static typing will not help you.
- I forgot to handle an edge case (I can use a discriminated unions for example to handle all cases explicitly)
You also forgot to write a test it sounds like. And if it affects your production code, you also forgot to think through your supervisor restart strategy.
- I did not check if something was null and pass it in to a function (does not happen in most ML languages)
Null isn't a special thing in Erlang. You can have the atom 'null' or 'undefined' or whatever, but that's an intentional assignment you create. And given the single assignment immutable nature of Erlang, you won't accidentally be setting/leaving things set to 'null'.
> unless you have a 3 parameter version of the function, in which case static typing will not help you
I half disagree. Let's say we go with the above scenario. If you have a three parameter function, static typing adds a check that the input parameter types need to match, if they don't, the program will not compile. Additionally, the returned type of the function may also not match, which would also be caught by the compiler, and depending on when the developer is writing the code, maybe immediately by the developer noticing that the type signature of the function does not match what they expect.
With dynamic typing, those checks have to happen at runtime or as a human written test. But if the compiler is smart enough to do it automatically, why not use that?
> You also forgot to write a test it sounds like. And if it affects your production code, you also forgot to think through your supervisor restart strategy.
Programming languages like F# have exhaustive pattern matching, which obviates the need for testing on whether all cases of a union type have been matched, because if they haven't, the compiler will emit a warning or error depending on one's language server settings. Whichever setting is used, the developer immediately knows they are missing a pattern match case at compile time.
>ngiven the single assignment immutable nature of Erlang, you won't accidentally be setting/leaving things set to 'null'.
This is similar to ml languages like F#. They are also immutable by default. F# calls it "unit".
With a statically typed language I (or my teammates, or people who write libraries I use) cannot forget to write a test for something like that because the type system ensures that it is caught. I don't have to worry about someone forgetting to write tests, and I don't have to wait until they've got a suitably large test suite to be certain of the guarantees the code can make.
> In Erlang this will also not compile, unless you have a 3 parameter version of the function, in which case static typing will not help you.
This is only true if the function you are calling is part of the same module (and is not called in module:func(Args)-form). If it's not, Erlang will happily compile it and you'll have to rely on xref to catch the error.
This is one of the compromises it takes to get hot code reload.
In theory what you say is equivalent, but in practice, thinking about these things at the design phase is far more easier than to handle them with testing (which is pretty forgiving if you miss things).
Probably there is a subset of extremely smart people who can write good and very reliable software in a dynamic language without going mad, but I am not among them. In the end I just left the language and now I am much happier with a mix of TypeScript, OCaml and Rust
Also the reliability they are talking about is quite different from the reliability that most people (especially ones who have not worked on erlang-y systems) think of when they hear the word. The Erlang reliability is about finding bugs and patching them (often in production) and not about avoiding them in the first place. Also it's about avoiding total system failure because of some bug in a part in it. This is a nice to have thing in all systems, but Erlang makes it easier to implement.
Of course, they also like to avoid bugs, but for that they use testing. And of course, we have to mention dialyzer which is something like TypeScript for Erlang but a lot less polished and with practically no tooling support. It is used in every significant Erlang project but the process is IMO quite sub-optimal and the price you pay (in suffering) is quite high.
This is kind of the key bit.
To caveat this - I'd like better typing in Erlang, and when i last used it in production I added type annotations to everything and ran Dialyzer as part of the build. But this is personal preference, and I'm not convinced it was the best use of my time; just the most conservative way to approach things for a system I needed extremely high uptime on.
You have to write tests anyway. Just because a statically typed system compiles doesn't mean your test load is reduced. While it eliminates certain classes of bugs, those same classes mostly get covered just by writing functional tests of sufficient coverage.
Between that, and Erlang's supervision tree (so that if you DO get a type error at runtime, it would be in a path you haven't adequately tested, and so not your expected 'happy path', and as such, can reasonably be restarted), it strikes me as a reasonable position to take.
It's not that you can't make good code in any language, but the cost is often not worth it; anything that makes it easier and more reliable saves you time and effort in the long run.
Static typing severely hinders hot code-reload, which was deemed more important for long running systems.
When I write Erlang, I'm not necessarily concerned with avoiding errors. The philosophy of the system is that errors will occur, and the focus is on making sure errors do not compound into system failure (supervision tree helps with this), that they're easy to see (appropriate logging of crashes), and that it's easy to deploy fixes (hotloading).
I'm sure there are many ways to avoid some errors in production, but more focus is on responding to errors than on preventing them. At least in my experience.
Will you not be testing your code anyways? Will you not have unit tests and integ tests and have it run in a devo environment first? Before going to production?
If so, then what would the gap be? If you catch those same type errors as you test, and you will be testing anyways?
The benefit just isn’t that prominent in a system built for the BEAM from what I’ve seen. And there are a bunch of negative trade offs that make distribution and hot reloading a lot more complicated.
Isn't BEAM already strongly typed? Did you mean a static type system?
Static analysis is great and required for modern development in dynamic languages and CPU/memory intensive to do well
These big companies should write their own LSP server since all editors support the LSP protocol and mandate their use in employee editors
Waiting till compile time to find that typo is rubbish, at runtime and compile time there should be as little checks as possible to keep performance up and the application running but on every keystroke in the editor it should be check city and again in CI
Instead Facebook keeps lagging up their dynamic languages by building incompatible languages with static analysis into the compile step fracturing communities for a worse solution
With a good IDE it's more pleasant than writing Elixir with typespecs and guards in that regard.
In the linked slides they say "We are working on a [static typing implementation (what exactly it will look like is still unclear afaik)] prototype, open-sourcing in November"