hpopp

hpopp

Access Behaviour on Structs

To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on structs.

https://github.com/codedge-llc/accessible

So, while I know this goes directly against core design decisions, it makes it extremely simple to perform deeply nested gets/updates on complex structs. This is something I could see myself using on every major project from now on.

I’ve found relatively little discussion about it, so I’m wondering, have I done something dirty?

Most Liked Responses

mguimas

mguimas

You can use put_in and get_in on structs like shown by Dogbert in Elixir: best practice to extract data from nested structs on Stackoverflow.

christhekeele

christhekeele

I think core’s design decision in this regard is twofold:

  • The compile-time guarantees of struct.field access should be strongly encouraged over the runtime struct[:field] form
  • The creator of the struct might have a different vision for what Access might mean for their data-structure

This is why it’s not a part of core—I don’t think that means that it should be avoided within your own structs, though, especially if they nest other data-structures you intend for your struct’s users to interact with manually.

An example of the second point might be a Conn struct that represents key/value access to an external system, like redis or an API. It’s going to want to define Access for itself, but with radically different intentions than exposing its actual literal fields.

lkuty

lkuty

Looks like it is for protocols, not behaviours. I think I will use struct_access to avoid copy/pasting:

@behaviour Access
defdelegate get(v, key, default), to: Map
defdelegate fetch(v, key), to: Map
defdelegate get_and_update(v, key, func), to: Map
defdelegate pop(v, key), to: Map

Note that the pop function might better be implemented as below to silently ignore the request to delete the key. Or we might want to raise.

def pop(v, key), do: {v[key], v}

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
New

We're in Beta

About us Mission Statement