Ah true, it’s not hard but does suck to have to convert Core to Erlang just to have debug info…
That does explain why LFE did not have debug_info support and why secondary hooks were often added. ^.^;
Fascinating design, I definitely need to catch up on modern developments on the engine.
Thanks @josevalim for sharing the idea for the below as well. This works better in our Slime partials:
@doc """
Save navigation for structs:
struct_get_in user, [:address, :city]
"""
def struct_get_in(data, key) when is_atom(key) do
struct_get_in(data, [key])
end
def struct_get_in(data, keys) when is_list(keys) do
{first, last} = Enum.split(keys, -1)
first = Enum.map(first, &Access.key(&1, %{}))
last = Enum.map(last, &Access.key(&1, nil))
get_in data, first ++ last
end
o = %OuterStruct{
inner: %InnerStruct{
field: 1
}
}
get_in o, [Access.key(:inner), Access.key(:field)] #returns 1
o = %OuterStruct{
inner: nil
}
get_in o, [Access.key(:inner), Access.key(:field)]
# returns
** (BadMapError) expected a map, got: nil
(elixir 1.12.3) lib/map.ex:464: Map.get(nil, :pairing, :a)
(elixir 1.12.3) lib/access.ex:459: anonymous fn/5 in Access.key/2
This happens because Access.key returns default only for a missing key. The :inner key is not missing, it is just set to nil so it is passed to next accessor.
I like that change a lot because it is more intuitive to me and makes the common problem of safely navigating easier to solve.
I hope it lands in 1.13 even though it is a breaking change for people depending on the BadMapError.