saverio-kantox
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
Some examples will help a lot, want to post some?
saverio-kantox
Here is some (writing from memory with a phone keyboard):
In my mind, the user shouldn’t know the specific type of C, the above code should produce (up to reordering, with is out of scope here)
You can observe that the top level A and the nested C are created automatically.
I think I can achieve this behaviour but I’m not sure whether it breaks some contract and whether there are better ways to allow the user to put values creating the intermediate steps as needed.
peerreynders
This involves using functions-as-keys in the path list that know what empty struct to supply when one is needed.
Access.keys/2does this for Maps and structs. Use its implementationhttps://github.com/elixir-lang/elixir/blob/v1.8.2/lib/elixir/lib/access.ex#L484-L497
as a blueprint for what you need to do in your particular case.
saverio-kantox
Very interesting, I haven’t thought about it. So the user instead of providing just the key path, would provide something like
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.
peerreynders
Example: