I prefer the previous answer, but another option would get a list of keys expected in the struct and take them from the map.
Building on the other example:
iex> Map.take(m, Map.keys(%URI{}))
%URI{
authority: "forum.elixirforum.com",
fragment: nil,
host: "forum.elixirforum.com",
path: nil,
port: 80,
query: nil,
scheme: "http",
userinfo: nil
}
Two downsides to this approach compared to the previous:
- This solution won’t replace any missing keys.
- This approach doesn’t let you create the struct dynamically (not without wrapping it in
struct/0at least, which at that point you might as well do the other).






















