hannesveit

hannesveit

Question about elixirc's static code analysis

Hi all,

I’m trying to get up to speed with elixir and have just stumbled across a compiler warning that I find a bit puzzling. I was wondering if someone here could explain to me why the compiler behaves like this. Consider the following code:

defmodule Foo do
  def f(), do: :foo
end

defmodule Bar do
  def f(), do: :bar
end

defmodule Test do
  def return_module_tuple_directly(name) do
    case name do
      "foo" -> {:ok, Foo}
      "bar" -> {:ok, Bar}
      _ -> {:error, :unknown_module}
    end
  end

  def return_module_tuple_with_module_variable(name) do
    module = case name do
      "foo" -> Foo
      "bar" -> Bar
      _ -> nil
    end
    if module != nil, do: {:ok, module}, else: {:error, :unknown_module}
  end

  def this_works_fine(name) do
    {:ok, module} = return_module_tuple_directly(name)
    module.f()
  end

  def this_fails(name) do
    {:ok, module} = return_module_tuple_with_module_variable(name)
    module.f()
  end

  def this_also_fails(name) do
    case return_module_tuple_with_module_variable(name) do
      {:ok, module} -> module.f()
      _ -> :error
    end
  end

  def but_this_works(name) do
    with {:ok, module} <- return_module_tuple_with_module_variable(name) do
      module.f()
    end
  end
end

If I compile that, I’m getting the following warnings:

    warning: nil.f/0 is undefined (module nil is not available or is yet to be defined)
    │
 34 │     module.f()
    │            ~
    │
    └─ modules.ex:34:12: Test.this_fails/1
    └─ modules.ex:39:31: Test.this_also_fails/1

What I don’t understand is:

  1. In this_fails/1, why does the compiler think the module could be nil? We’re explicitly testing if module != nil. So it should know that the module can never be nil if the first tuple element is :ok. I’m assuming it’s smart enough to “see” that the value could be nil from the case statement but then it’s not smart enough to also introspect the condition of if (module != nil)?

  2. Why does wrapping the matching of {:ok, module} in a with statement prevent this warning, but the similar variant with the case statement does not? This is what confuses me the most.

I’d really appreciate an explanation! Thanks!

Most Liked

LostKobrakai

LostKobrakai

This looks like a bug in the typesystem, though it’s a bit surprising that using with would make things work.

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
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
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement