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 empty should be done by using
Enum.empty?(enum)
or
list ==
My question is, why isn’t the compiler smart enough to fix it “under the hood”? Seems odd.
I believe Scala does exactly that.
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Other Trending Topics
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
Its not the compilers task to lint your code. This has nothing to do with “smartness”, its just a matter of responsibilities.
michalmuskala
The expressions
length(list) == 0andlist == []have slightly different behaviour when the variablelistisn’t actually a proper list:length(list)will fail if the input is not a proper list and this means that the whole clause will fail - even it might seem it would succeed otherwise - e.g.length(value) >= 0used as a guard would fail for input[1 | 2].list == []will just return false for anything that is not an empty list.Because of this difference, such a conversion is not safe to be performed automatically by the compiler - it’s extremely hard to prove it would be safe. A compiler has to be 100% sure an optimisation is safe to perform it. On the other hand, in 99% of cases it actually is safe and the programmer can switch it.
bsollish-terakeet
Thanks for the response, I hadn’t considered improper lists.
The problem, however, as I see it is that if I do (somehow) have an improper list:
foo = [1 | 2]
and I run the following checks:
is_list(foo)
Enum.empty?(foo)
I come to the incorrect conclusion that I have a list with one or more items.
Performance implications aside, it almost seems better/preferable to do length(foo) == 0, because at least that blows up with an argument error. Neither of the Credo suggestions do.
benwilson512
Improper lists are still lists, this isn’t an incorrect conclusion. If you want to make sure something is a proper list, do a dedicated check for that. If you want to know if a list is non empty, checking equality with
[]is the clearest thing to do.bsollish-terakeet
RE: “… checking equality with
[]is the clearest thing to do.” I could have just as easily said foo = instead of Enum.empty?(foo) in my example. As Credo recommends either, they need to be functionally equivalent.RE: “If you want to make sure something is a proper list, do a dedicated check for that.”
How do you check for an improper list? (i.e. differentiate [1 | 2] from [1 | [2, 3]])
benwilson512
It’s nearly never worth doing this check though.