bartblast

bartblast

Creator of Hologram

Typing violation warning when comparing atoms

Why comparing atoms gives typing violation warning?

consider:

defmodule MyModule do
  def test do
    :a < :b
  end
end

mix compile my_module.ex

gives a warning:

Compiling 1 file (.ex)
    warning: comparison between incompatible types found:

        :a < :b

    While Elixir can compare across all types, you are comparing across types which are always distinct, and the result is either always true or always false

    typing violation found at:
    │
  3 │     :a < :b
    │        ~
    │
    └─ my_module.ex:3:8: MyModule.test/0

First 10 of 13 Posts! Switch mode

tcoopman

tcoopman

What’s the result you expected? What are you trying to do?

antoine-duchenet

antoine-duchenet

Because :a and :b are not considered to be of type atom() but respectively as type :a and :b.

LostKobrakai

LostKobrakai

And as mentioned in the warning the result of the comparison is static as well.

bartblast

bartblast

Creator of Hologram

Since the < operator is inlined with :erlang.</2, I expected the compiler to resolve its typing to a parametric polymorphic definition, something akin to (a, a) -> boolean() when a: term(). I didn’t expect singleton types.

I gave a simplified example to illustrate the problem, which I encountered when implementing client-server consistency tests in Hologram. Basically, I test whether manually transpiled Erlang functions behave exactly the same as their server counterparts.

LostKobrakai

LostKobrakai

If the typesystem has distinct types, why would it fall back to less specific types? This would be different if you would have variables here, but you hardcoded values for the comparison.

bartblast

bartblast

Creator of Hologram

@antoine-duchenet @LostKobrakai Guys, considering your previous answers, does this mean that Erlang functions are not treated as “atomic” functions for the type system and they don’t have hardcoded typings? Or maybe the underlying Erlang functions’ abstract code (or bytecode) is analysed as well in the process to determine the typings?

LostKobrakai

LostKobrakai

Not sure I get what you’re asking for, but I don’t think there’s much to it.

With 1.17 warnings where implemented to warn when comparisons are made between different types of data – mostly to warn for the common footgun of comparing structs like DateTime, which would use structural comparison not semantic one. That’s the source of the warning.

The reason for getting the warning if both sides are atoms comes from the fact that the new typesystem implementation considers atom() a divisable type, which includes all possible individual atoms. So :a and :b are distinct types based on the typesystem, but both are part of atom(). The wording sounds a bit unfortunate here though given it’s a bit ambiguous what type of “type” the error refers to.

dimitarvp

dimitarvp

He’s probably asking about BIFs and such.

LostKobrakai

LostKobrakai

BIFs are a runtime/implementation concern though. The compiler (and the typesystem) generally don’t care about any of that.

dimitarvp

dimitarvp

So your functions that call them would never be type-checked by Dialyzer whether they pass the right types of arguments to BIFs, is that what you’re saying?

Last Post!

sabiwara

sabiwara

Elixir Core Team

I’m not sure if I understand your question correctly, but functions applications (Foo.bar(baz) or :foo.bar(baz)) are not typed yet, this will come with future releases.

Standard library functions from Elixir and Erlang will be typed then.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement