fireproofsocks

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:

  1. Is there an example somewhere in some library that demonstrates how to implement the Access behaviour?
  2. 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!

Showing Posts 1 to 10

NobbZ

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

kip

ex_cldr Core Team

I think you’ll find this will do the trick:

iex> get_in(my_struct, [Access.key(:x, Access.key(:y, "my_default"))])
%{y: "My Value"}

You can wrap struct keys in Access.key/2 if required without implementing the whole Access protocol.

fireproofsocks

fireproofsocks OP

That looks promising… however, it seems to return not the value for :y (or its default), but instead, the entire map stored at :x :thinking:

kip

kip

ex_cldr Core Team

Wasn’t quite sure what you were after. Probably you wanted:

iex> my_struct = %MyStruct{x: %{y: "this thing"}}
%MyStruct{x: %{y: "this thing"}}
iex> get_in(my_struct, [Access.key(:x), Access.key(:y, "my_default")])
"this thing"

Note that since :x is 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 a struct:

iex> get_in(my_struct, [Access.key(:x), :y])                          
"this thing"
fireproofsocks

fireproofsocks OP

That works! I think I tried a dozen things that were close to that. Thank you!

msante

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:

  def destructuring(struct) when is_struct(struct) do
    struct |> Map.from_struct() |> destructuring
  end
  def destructuring(list) when is_list(list) do
    list |> Enum.map(fn v ->
      destructuring(v)
    end)
  end
  def destructuring(map) when is_map(map) do
    map |> Map.keys() |> Enum.reduce(%{}, fn (k,acc) ->
      Map.put(acc, k, destructuring(map[k]))
    end)
  end
  def destructuring(val), do: val

so when I need it I can do:

iex> my_struct |> destructuring() |> get_in([:x, :y, :z])
nwjlyons

nwjlyons

Elixir 1.17 added get_in/1 (Note the arity 1) which works with structs.

get_in(my_struct.x.y) || "my_default"
msante

msante

I’m new to elixir, but wouldn’t this be the same as doing this?

my_struct.x.y || "my_default"

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

frankdugan3

No, because my_struct.x.y would raise an error if my_struct or x were nil. get_in/1 provides nil-safe access, similar to Javascript’s myObject?.x?.y.

Sorry, just realized I didn’t account for the logical ||. So for this specific case, both should do the same thing.

nwjlyons

nwjlyons

If x is nil doing .y will raise a KeyError. get_in/1 wont raise a KeyError

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New
adr
Hello all. I have a question as to best practices for code organization when implementing a liveview with a lot of responsibilities. Sup...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews