user974881

user974881

DETS keypos option to store a struct

Hello,

I’m trying to use DETS to store a struct, so I know that for this, I need to use the keypos option to tell elixir where it should get the key, so it’s value would be #‍struct.key, but in elixir, I don’t know how to do it.
I have tried :key, %Struct.key, %Struct[}.key, #‍struct.key and also without keypos, but none have worked.

How do I use DETS to store a struct? I have also checked in Elixir’s source code and documentation but haven’t found it, I guess this is because it comes from erlang.

Marked As Solved

al2o3cr

al2o3cr

The #record.key syntax is for records; Elixir structs are not records, they are maps.

DETS stores tuples, so records fit naturally.

You’ll need to either produce a tuple from your struct, so that %SomeStruct{key: 123} is stored as {123, %SomeStruct{key: 123}} or transform the struct to a record (see the Record docs for some Elixir helpers).

One possible gotcha: for the “convert to a record” case, I don’t see a clear Elixir equivalent to #record.key to get a tuple position from a field name :thinking:

Edit: thanks to @lud for calling out what I totally skimmed past - if you said defrecord :user, ... then user(:some_field) is the zero-based index of some_field in the record. That will work as-is if you want to access the field with Kernel#elem, but you’ll need to explicitly add one if you pass it to ETS as keypos (which expects a 1-based index!).

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement