I want to check if the type of a parameter given to a function in Elixir is a Dictionary. How do I do this?
Asked
Active
Viewed 694 times
1 Answers
2
First you have to be aware that Elixir supports 2 Dictionary types
- Erlangs native Map type (for maps with only limited items)
map = %{}
- Elixirs own Dictionary type (dictionaries with a potentially large payload)
dict = HashDict.new
Both types however need to be checked with Erlangs native :erlang.is_map
.
def some_fun(arg) when :erlang.is_map(arg) do
#do your thing
end
More info can be found under sections 7.2 and 7.3 (http://elixir-lang.org/getting_started/7.html)

robkuz
- 9,488
- 5
- 29
- 50
-
1HashDict will return true to :erlang.is_map/1 just because it's a struct. You will get true for example if you run :erlang.is_map(%URI{}). URI is a struct also, but it will return true. If you want to check if it's a HashDict you should pattern match on it with something like this: def some_fun(arg = %HashDict{}), do: ... You can see more here: https://github.com/elixir-lang/elixir/blob/master/lib/elixir/lib/hash_dict.ex#L40 – Eduardo Oct 06 '14 at 02:11