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
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
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
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #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.