In Elixir I can check if a variable is a map
or a struct
, by calling Kernel.is_map/1
which makes sense because Structs are Maps underneath, but I'd like to differentiate between the two. I know that I can call __struct__
on a Struct to get it's module name but calling it on a normal map throws:
** (KeyError) key :__struct__ not found in: %{}
So my question is, How do I check if a variable is a map or a struct?
Example use case:
# I want to handle struct and map inputs differently in my Module
defmodule DifferentThings do
def do_something(arg) when is_map(arg) do
# Do something with Maps
end
def do_something(arg) when is_struct(arg) do
# But handle Structs differently
# Issue is, `is_struct` does not exist
end
end
In general to check if map is a struct:
Map.has_key?(struct, :__struct__)
For different method declarations (more general method is second):
defmodule DifferentThings do
def do_something(%{__struct__: _} = arg) do
# ...
end
def do_something(arg) when is_map(arg) do
# ...
end
end
It is possible to pattern match between struct and map like below
defmodule DifferentThings do
def do_something(arg = %_x{}) do
IO.puts "This is a struct"
end
def do_something(arg = %{}) do
IO.puts "This is a map"
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With