Understanding @spec in Elixir and Seeking Recommendations for IDE Tools

Hello and welcome to the forums.

In short, Elixir is maybe probably getting a type system but as it stands it does not have one. You can use Dialyzer, via the Dialyxir package, to do type checking with @specs. I personally don’t have much experience with it so can’t give you any pointers here, but I believe it can help with the atom/string key situation (eg, @spec foo(%{atom() => any()}) :: any()). Otherwise context is usually helpful for knowing what they will be. In short, it will pretty much almost always be atom keys unless the data is being parsed blindly from an untrusted source. In this case they will be (at least they should be) string keys as dynamically creating atoms is a big no-no: any on BEAM instance can only define a finite number of atoms an when it runs out, bad things happens. Often untrusted data will be cast into a known shape in which case the keys are generally converted to known atoms—Ecto does this, for example.

2 Likes