lud

lud

Please explain inconsistency with :erlang.map_get(key, map) in guards

Hi,

If in a guard I use :erlang.map_get/2 on a map that does not have the key, and use a or to put another guard condition in second position, the second condition will not be evaluated.

defmodule T do
  def check_nil_1(map, key) when :erlang.map_get(key, map) == nil or not is_map_key(map, key),
    do: true

  def check_nil_1(_, _),
    do: false

  def check_nil_2(map, key) when not is_map_key(map, key) or :erlang.map_get(key, map) == nil,
    do: true

  def check_nil_2(_, _),
    do: false
end

map = %{a: 1}
key = :b

T.check_nil_1(map, key)
|> IO.inspect(label: "check 1")

T.check_nil_2(map, key)
|> IO.inspect(label: "check 2")

# Output:
# check 1: false
# check 2: true

I expected both checks to be true. The only difference is the order of the conditions in the guards. The documentation says:

The call fails with a {badmap,Map} exception if Map is not a map, or with a {badkey,Key} exception if no value is associated with Key. […] Allowed in guard tests.

Obviously as it is in a guard it does not fails with an error, but the docs do not tell that it also skips further evaluation of the guard conditions.

So, are there any more docs on that subject ? And any other special cases in guards that would be good to know ?

Thank you

(edit: if the key exists in the map with a nil value, both checks return true as expected.)

Marked As Solved

josevalim

josevalim

Creator of Elixir

map_get raises if the key does not exist on the map and or only executes the right-side if the left-side is false. Therefore, in the first case, when you do the map_get for a key that does not exist on the map, it will fail and never execute the right-hand side of or.

Also Liked

Qqwy

Qqwy

TypeCheck Core Team

As an alternative if you want a particular function clause to be called even if an earlier guard of this clause raises, you might use multiple guards in the same clause.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
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
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "eq...
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
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47849 226
New

We're in Beta

About us Mission Statement