saverio-kantox
Access on structs with typed members
In a project I’m working in, I have to create a boatload of different structs, each containing a list of different types of objects. Think XML with types. The values cannot be set directly on fields because order must be preserved, so there is no explicit type spec on the struct (it has one field that contains a kwlist with the children)
So we are implementing Access on them, to be able to get lenses on them, but I would like the user of these type to not have to know beforehand what type should be put in each child.
What would be the natural way to expose to the user a “blueprint” of the correct struct for a key?
Can some of the callbacks for Access expose an empty struct of the correct type instead of nil?
Is it reasonable for the user to expect that put_in and related functions do initialize intermediate objects when needed? (In the style of mkdir -p)
Thanks for your opinions and suggestions.
Marked As Solved
peerreynders
Example:
defmodule Nav do
def a,
do: key(:a, [])
def c,
do: key(:c, [])
def some,
do: key(:some, [])
defp key(key, default) do
fn
:get, data, next ->
next.(Keyword.get(data, key, default))
:get_and_update, data, next ->
value = Keyword.get(data, key, default)
case next.(value) do
{old, update} ->
{old, Keyword.put(data, key, update)}
:pop ->
{value, Keyword.delete(data, key)}
end
end
end
end
thing = []
path = [Nav.a(), Nav.c(), Nav.some()]
new_thing = put_in(thing, path, "value")
IO.puts("#{inspect(new_thing)}")
update = &{&1, "more " <> &1}
{old_value, more_thing} = get_and_update_in(new_thing, path, update)
IO.puts("#{inspect(old_value)} #{inspect(more_thing)}")
result = get_in(more_thing, path)
IO.puts("#{inspect(result)}")
{deleted_value, pop_thing} = pop_in(more_thing, path)
IO.puts("#{inspect(deleted_value)} #{inspect(pop_thing)}")
$ elixir nav.exs
[a: [c: [some: "value"]]]
"value" [a: [c: [some: "more value"]]]
"more value"
"more value" [a: [c: []]]
Also Liked
saverio-kantox
Very interesting, I haven’t thought about it. So the user instead of providing just the key path, would provide something like
put_in(thing_a, [
MyAccess.key_or_create(:a),
MyAccess.key_or_create(:c),
…
], "value")
and the functions will be created based on some protocol that I need to define and implement?
Makes total sense, I did not feel 100% comfortable in returning non-nils on missing keys.








