Let say I have a struct, struct = %MyApp.MyModel{ filled_with_data: "true }
.
How can I get struct name (MyApp.MyModel
in my case)?
It's stored in a field named __struct__
, so struct.__struct__ == MyApp.MyModel
.
You can also get the struct name by pattern matching:
%name{} = struct
:__struct__
map key, which is an implementation detail.
Commented
Sep 23, 2022 at 13:51
If you need the name of the struct as a string without the "Elixir." prefix, but keeping any of your module prefixes you can do:
name = your_map.__struct__
|> Module.split
|> Enum.join(".")
your_map.__struct__
(where your_map
is an Ecto.Schema.t
struct), I didn't get the Elixir.
prefix in the result just now in iex
.
Commented
Jan 14, 2020 at 20:18
Structs carry __struct__
field in them. So,
struct.__struct__
should do