Skip to content

Add map_get/2 guard #12281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

zolrath
Copy link

@zolrath zolrath commented Dec 2, 2022

Hello!

Erlang/OTP 21 added both is_map_key/2 and map_get/2, but currently only is_map_key/2 is exposed via Elixir guards.

As most users will likely look through Elixir documentation to discover which guards are available, exposing map_get/2 seems appropriate.

This PR adds a map_get/2 guard, exposing :erlang.map_get/2 to aid in discoverability.
The argument order has been changed to (map, key) for consistency with is_map_key/2 and the Map module.

I've defined map_get/2 as a macro, as if I simply def and immediately delegate to :erlang.map_get/2 tests fail with:

error: cannot invoke remote function Kernel.map_get/2 inside guards
  lib/elixir/test/elixir/kernel_test.exs:1521: KernelTest."test map_get/2"/1

Thanks for everything!

Adds a `map_get/2` guard exposing `:erlang.map_get/2`.

The argument order has been changed to `(map, key)` for consistency with `is_map_key/2` and the `Map` module.
@zolrath
Copy link
Author

zolrath commented Dec 3, 2022

Ah, just found a discussion from 2018 on ElixirForum about this! Am I correct reading "We are going with #1" and the fact that is_map_keys exists that this is intended to be added as well?

The code that triggered the discussion around this guard/this pull request was during Advent of Code answer discussions:

  @winners %{rock: :scissors, paper: :rock, scissors: :paper}

  def battle(same, same), do: :draw
  def battle(them, you) when :erlang.map_get(you, @winners) == them, do: :win
  def battle(_, _), do: :lose

As it's one of the few erlang guards not listed in the docs, there was some "oh hey that guard exists?" discussion.

@josevalim
Copy link
Member

Correct. We know it exists, we know it is missing, but we are skeptical about adding a fourth way of reading map keys (map.key, map[key], Map.get). We may consider adding other ways in the future, such as pattern matching.

@josevalim josevalim closed this Dec 3, 2022
@zolrath
Copy link
Author

zolrath commented Dec 4, 2022

Correct. We know it exists, we know it is missing, but we are skeptical about adding a fourth way of reading map keys (map.key, map[key], Map.get). We may consider adding other ways in the future, such as pattern matching.

Sounds good to me! If the above map_get could be expressed with map[key] that would definitely feel natural.

def battle(them, you) when @winners[you] == them, do: :win

@zolrath zolrath deleted the add-map_get-guard branch December 29, 2022 04:51
@girlsavenue girlsavenue mentioned this pull request Dec 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants