Gradually gradually typing Elixir
Our old friend José Valim & his team have been hard at work adding gradual typing to Elixir. They’re only 1-3% of the way there, but a lot of progress has been made. So, we invited him back on the show for a deep-dive on why, how & when Elixir will be gradually typed.
Matched from the episode's transcript 👇
José Valim: So before that tweet, let’s say, Elixir was a dynamically-typed language. And one of the ways that you can think of a dynamically-typed language is that it’s a “statically-typed” language where all the functions, all the inputs are dynamic, and then it returns dynamic. So everything that you receive is dynamic, everything returns dynamic, and dynamic means “I don’t know what is the type at compile time. I know at runtime.” So if I try to add a number to a list, which is not possible in most programming languages, you are supposed to – in Elixir you’re going to get an error at runtime. But if you had a static typed system, you could potentially catch that error at compilation time.
So Elixir was a dynamically-typed language. And being a gradually-typed language means that you have the dynamic, but at the same time you have the dynamic, you can also have precise type information. So what the tweet was saying is that “Well, instead of Elixir analyzing all the variables, all of your inputs as dynamic, now we are doing inference on your patterns and guards, which are very common in Elixir code.” So in your pattern, you can say “Oh, I’m going to match on a list. You can use a guard to say “Oh, this thing is a number, or an integer.” We are getting that information to say “Oh, so now I know that this variable is no longer dynamic, and it’s actually an integer, and it’s a list.”
And now that we are mixing this dynamic world with this little bit of type information, now it’s officially gradually-typed. And that’s correct, but it’s more it’s officially gradually-type, but we are at 1% of the journey. And that’s what the following tweets meant to say.
So if you think about everything you can do in a pattern and in a guard in Elixir - you can check if it’s an integer, if it’s a function, you can pattern-match on maps, on tuples, on lists - we only understand one construct right now, which is the binary matching and the binary construction. So of all the things in the language, we understand one of them. But getting to this one is super, super-hard. It’s literally two years of research have passed… So getting to this one was super, super-hard. Now getting to the next ten is not going to be as hard… But yeah, we are officially gradually-typed, but it’s we are 1% gradually-typed, if that makes sense.