fireproofsocks
I stumbled across the desire to want to use get_in while accessing deeply nested parts of a custom struct while needing to provide a default value. I could make a simple defp function to achieve this, but I wanted to see how to handle it using the Access protocol, so I tried something like this:
get_in(my_struct, [:x, Access.key(:y, "my_default)])
Where my struct was shaped something like
%MyStruct{x: %{y: "My Value"}}
# or maybe
%MyStruct{x: %{}}
This results in an error:
** (UndefinedFunctionError) function MyStruct.fetch/2 is undefined (MyStruct does not implement the Access behaviour)
I have 2 followup questions to this:
- Is there an example somewhere in some library that demonstrates how to implement the Access behaviour?
- I’ve read somewhere that structs don’t implement this behaviour for performance reasons – what considerations should be made before implementing the behavior in a struct?
Thanks as always!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
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
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
Not performance I’d say… Semantic reasons. It doesn’t make sense for all structs.
Also let’s take a fictive set which implements access and returns either true or false, depending on if the value is a member.
The struct itself has only a single field, which shall not be made accessible, but instead access is just a
Map.get(s.m, value, false).I’m not aware of any examples though, but perhaps you can find something in elixir code base for maps or keywordlists.
kip
I think you’ll find this will do the trick:
You can wrap
structkeys inAccess.key/2if required without implementing the wholeAccessprotocol.fireproofsocks
That looks promising… however, it seems to return not the value for
:y(or its default), but instead, the entire map stored at:xkip
Wasn’t quite sure what you were after. Probably you wanted:
Note that since
:xis just a map you can do the following if you don’t need to supply a default (which you shouldn’t have to so since this is about astruct:fireproofsocks
That works! I think I tried a dozen things that were close to that. Thank you!
msante
I came here looking for a simple way to implement Access behaviour by all structs, but I agree with @NobbZ, it doesn’t make sense to do it for all structs.
So I thought of a simpler solution, just ‘unstructure’ those structs where I need to use get_in to access them deeply.
I made this little function:
so when I need it I can do:
nwjlyons
Elixir 1.17 added
get_in/1(Note the arity 1) which works with structs.msante
I’m new to elixir, but wouldn’t this be the same as doing this?
Apart from this, I wonder how I could take advantage of get_in/1 in the case that the list of keys has to be generated dynamically based on certain logic?
frankdugan3
No, becausemy_struct.x.ywould raise an error ifmy_structorxwerenil.get_in/1provides nil-safe access, similar to Javascript’smyObject?.x?.y.Sorry, just realized I didn’t account for the logical
||. So for this specific case, both should do the same thing.nwjlyons
If
xisnildoing.ywill raise aKeyError.get_in/1wont raise aKeyError