How to check if a Map is also a Struct?
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
You can't have separate function heads for Map vs Struct with a guard, but you can do it with pattern matching.
defmodule Guard do
def foo(%{:__struct__ => x }) do
Struct
end
def foo(x) when is_map x do
Map
end
end