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.18.4

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

New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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 42633 214
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement