5 ms·
> Are many of the other languages you've used strongly typed? That's the thing that gives me pause - I feel like I rely on the compiler a lot in languages where
by QuinnWilton 6y ago
> Are many of the other languages you've used strongly typed? That's the thing that gives me pause - I feel like I rely on the compiler a lot in languages where it can do a lot for me, and that I'd be really frustrated managing a large project without it.
As someone who has been writing Elixir professionally for 4-ish years, and who is otherwise about as big of a proponent of static types as is possible, this is simultaneously my biggest critique of Elixir, and also a critique that has ended up being more theoretical than not. I wish that it were statically typed, but in practice that hasn't been slowing me down.
Part of the reason for that is that while Elixir is dynamically typed, I've found that it's possible to pretend it is a statically typed language if you squint just right. By that, I mean making judicious use of typespecs [0], dialyzer [1], Norm [2], and just generally constraining the way you write code to mirror the sorts of code that you'd write in a language that offers algebraic data-types, like Haskell. I've been meaning to put together a blog post on what I mean by this, because I often hear people talking about how unusable Dialyzer is, so I feel like my team is a rare example of a large production app that requires that Dialyzer typechecks on every build. This makes large-scale refactoring of our app almost (but not quite) as easy as it would be in Haskell.
The macro system in Elixir also means that it's possible to write libraries like typeclass [3], that offer compile-time guarantees (using compile-time property tests!) that your implementations correctly implement a given interface. I think that as the language evolves, we'll be seeing a lot more examples of macros that move runtime logic into the compilation step, to offer compile-time guarantees and safety. For example, a few weeks ago, I prototyped an experimental library in a few hours that added support for compile-time OCaml style parameterized-modules. [4]
Obviously, all of these techniques don't get you quite as far as proper static typing would (though the Whatsapp team is working on static types for Erlang! [5]), but the rest of the language and the ecosystem is just so well thought out, that I've been okay with that!
[0] https://hexdocs.pm/elixir/typespecs.html https://hexdocs.pm/elixir/typespecs.html
[1] https://github.com/jeremyjh/dialyxir https://github.com/jeremyjh/dialyxir
[2] https://github.com/keathley/norm/ https://github.com/keathley/norm/
[3] https://github.com/witchcrafters/type_class https://github.com/witchcrafters/type_class
[4] https://github.com/QuinnWilton/daat/ https://github.com/QuinnWilton/daat/
[5] https://www.facebook.com/careers/jobs/229713254864749/ https://www.facebook.com/careers/jobs/229713254864749/
- scythmic_waves 6y agoTo add to this, it's worth noting that Elixir would have difficulty _ever_ becoming statically typed because of how message-passing works. For example, if you look at Gleam [0] -- the recent project that's trying to create a statically typed language for the BEAM -- it can't handle message-passing [1]. And I totally agree with you here: > I've found that it's possible to pretend it is a statically typed language if you squint just right. My Elixir code tends to use a lot of pattern matching on structs. It's not real static typing, but it gets me a lot of mileage! [0] https://gleam.run/index.html https://gleam.run/index.html [1] https://gleam.run/faqs.html#how-is-message-passing-typed https://gleam.run/faqs.html#how-is-message-passing-typed
- QuinnWilton 6y agoI don't know if I'd say "ever", but it definitely won't be easy! Take a look at session types [0] if you haven't seen them! Type checking unrestricted message passing would be difficult, but I can imagine a subset that's typeable. [0] http://groups.inf.ed.ac.uk/abcd/index.html#about http://groups.inf.ed.ac.uk/abcd/index.html#about
- scythmic_waves 6y agoI haven't seen them, thanks! > Type checking unrestricted message passing would be difficult, but I can imagine a subset that's typeable. I'd love to see a discussion on this somewhere. I can totally imagine a world where it's true that "yes, you can't send messages of type X because the receiver can't pattern match on them in the way you expect, but what you're really trying to do is Y, and you _can_ send messages of type Z that accomplish Y."
- lpil 6y agoHi, I'm the author of Gleam. We have fully type safe message passing, we just don't have any special syntax for it. I'll update the documentation to make this clearer.
- scythmic_waves 6y ago