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.

Last Post!

hannesveit

hannesveit

FWIW,

$ elixirc --version
Erlang/OTP 28 [erts-16.0.1] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit] [dtrace]

Elixir 1.18.4 (compiled with Erlang/OTP 27)

Where Next?

Popular in Questions Top

hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
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 empt...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement