VictorGaiva

VictorGaiva

No escape hatch for typing error

I’m currently migrating my codebase to Elixir 1.18.2. I was able to solve all the typing issues, except for one. The following code:

defmodule Run do
  def test do
    try do
      Code.compile_string("a + ")
    rescue
      e ->
        case e do
          %{column: column, snippet: %{offset: offset}} ->
            {:error, column + offset}

          _ ->
            {:error, :unknown}
        end
    end
  end
end

Gives me the following compilation warning:

This could be solved by adding guards, that will allow for infering the types in a future release. But at the moment I’m not able to migrate this code and there is no scapehatch for it.

First Post!

VictorGaiva

VictorGaiva

Ah, I was able to workaround this warning by creating a ‘sum/2’ function. Since the type checking doesn’t flow throught functions, it works fine.

Most Liked

VictorGaiva

VictorGaiva

Sure

defmodule Run do
  def test do
    try do
      Code.compile_string("a + ")
    rescue
      e ->
        case e do
          %{column: column, snippet: %{offset: offset}} ->
            {:error, sum(column, offset)}

          _ ->
            {:error, :unknown}
        end
    end
  end

  def sum(a, b) do
    a + b
  end
end

Where Next?

Popular in Questions Top

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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement