AppleKid7

AppleKid7

What's state of the art as far as the type system and/or static analysis?

Hello again, I’m still in the process of learning Elixir. Almost done with Dave Thomas’ “Elixir for Programmers” course and I’m really enjoying it so far. But the course is a few years old and things seem to have changed a bit since. I’ve been following it using Elixir 1.17 and Phoenix 1.7.21 and I’ve had to tweak several things that don’t seem to be done the same way anymore, but the docs and even the help of Claude for specific questions have made the process very smooth. Having not really done a project by myself, let alone one that’s running on a production environment, I feel I’m still new to Elixir (my next step is to learn Phoenix more in-depth and try to develop an app on my own that will be reachable to the “outside world”) so I hope this isn’t a stupid question… But I absolutely love algebraic data types for modeling lots of domains and ran into this article:

Now I see the article is 3 years old and I’m also not sure how common this terminology and pattern are as far as idiomatic Elixir goes… My experience with Dialyzer is that if I add no flags, it’s a bit too permissive (it let me get away with writing multiple specs for the same function name with the same arity, which I was later told is a no-no), so I also tried to add various combinations of these flags:

  • "-Wunmatched_returns"
  • :error_handling
  • :underspecs
  • :overspecs
  • :specdiffs
  • :overlapping_contract

And ran into various issues with stuff that to me looked like it should’ve been correct but according to dialyzer the success typing was something way more specific that sometimes I tried to bend over backwards to accommodate, but it just wasn’t practical in some cases. I also had a bit of an issue with structs that had default values (e.g. if I set a field age to have default value 25 and in my spec I tried to tell it was an integer(), when I used dialyzer with the flag :specdiffs it would give me an error that the type wasn’t integer() but was 25 instead). So in the end I opted to just exercise caution and discernment and kept dialyzer as it is by default, without any of those extra flags…

I also know that Elixir 1.18 already includes the gradual set theoretic type system. So I guess I’m wondering what the state of the art might be when it comes to algebraic data types, exhaustive pattern-matching, etc, or if they’re even idiomatic to Elixir or desired/necessary at all. I can see the case for it not being necessary since you can have your functions match the inputs you’re expecting and anything other than that will just not match and give an error and it’ll be no big deal because it’s not like it’ll crash your entire VM as it could happen with other languages…

But in case they are considered good practices of sort, I’d love to read more resources on how to make things work more smoothly… perhaps the state of the art when it comes to balancing version >=1.18’s type system with dialyzer?

…or I’d love to just hear what experienced Elixir devs’ thoughts are on the matter :slight_smile:

Most Liked

sabiwara

sabiwara

Elixir Core Team

Re: dialyzer flags.

While the old overspecs and underspecs flags are quite unusable in practice in my experience due to false positives, the more “recently” released (OTP25) missing_return and extra_return flags cover a useful subset of errors.
They improve Dialyzer’s ability to catch bugs without the false positives (or almost), and I think offer a good trade-off.

sbuttgereit

sbuttgereit

To be clear, it contains part of it, not all of it. I just recommended this link in a different thread, but the link has relevance here, too: Gradual set-theoretic types — Elixir v1.20.2

I’d look at that for what’s done and what’s not done but on the roadmap.

AppleKid7

AppleKid7

Thanks, I’ll definitely check that out! I read the release notes of 1.18.0 back in December and it seems very promising! Seems like you get the type checks without necessarily affecting how you write your code.

TYSM! I’m playing around with the missing_return and extra_return flags and so far it’s getting along with my code, so that might be a good sign :grinning_face_with_smiling_eyes:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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 43757 214
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement