jwinnie

jwinnie

Phoenix, type annotations and Dialyzer

Elixir is a great language, but its lack of static types is a major drawback for me.

Dialyzer seems like a good way to fix this issue, but it seems like Phoenix doesn’t provide type annotations (at least that’s what I gather from reading the source code on GitHub), which makes type-checking essentially useless (it doesn’t matter if my code has types, if my framework doesn’t).

Is there any reason the Phoenix framework lacks types? Would resolving the issue be as easy as adding type annotations to everything? If so I would love to help out.

Most Liked

LostKobrakai

LostKobrakai

That’s not really the case. Dialyzer is far away from what other static type systems provide. Elixir will stay an dynamically typed language even if you’re using dialyzer.

That’s not the case as well. Dialyzer doesn’t need explicit typespecs at all – to the contrary. Dialyzer does infer types everywhere. It’s more of a sanity check in dialyzer to compare the inferred types with what was supplied by typespecs and error if there’s a mismatch detected.

dimitarvp

dimitarvp

Dialyzer and @spec are, I believe, largely deemed to be mostly useful for code documentation purposes. Using type_check or norm should be better for your original goal – although no true compile-time static type checking in this ecosystem as you noted.

krstfk

krstfk

This is not exactly true. Dialyzer will fail when you add a level of indirection: eg (from the little elixir otp book) :

def add(a,b) do 
   a + b 
end 

def inc(b) do
  add(1, :foo)
end
   

will trip dialyzer

But

def add(a,b) do 
   a + amount(b)
end 

def amount(x), do: x

def inc(b) do
  add(1, :foo)
end
   

Will not (ok, maybe it will, this is of the top of my head but the point stands). And then you can help it with specs.

The thing is, dialyzer is NOT a static type checker, it is (imho a very useful) static analysis tool, that does success type checking and some type inference.

As I’ve said elsewhere, it shouldn’t preclude writing typespecs as they are really useful for your colleagues and your future self (I can remember what my get_some_stuff() function does but I’m very bad at remembering whether it returns {:ok, stuff} or just stuff and raises if nothing comes up).

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement