vrod
Sorry because many other people asked a similar question… but how do you implement the Enumerable protocol for a struct? I see the docs on https://hexdocs.pm/elixir/master/Enumerable.html but I do not follow how to actually do this… is there an example somewhere? Just to be able to treat my structs the same as maps for many Enum operations. Thank you!
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
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
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
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
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
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
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
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
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
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
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
(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
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
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #elixirconf-us
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
seanmor5
Why not just use Map.from_struct/1?
eksperimental
The best example is the source code itself.
https://github.com/elixir-lang/elixir/blob/ae5188d8996f9ab44358be3d7acd4447a2de0677/lib/elixir/lib/enum.ex#L3986-L4007
But note that you cannot rely on the implementation for maps. The only function that will work is
member?/2vrod
2 reasons I cannot use from_struct:
srowley
Speaking totally naively, it seems like you could start with the implementation for maps, and then just add a step for each callback that addresses how the
{:__struct__, ModuleName}key/value pair that is a part of every struct is handled. I can imagine cases where you might want to ignore it, and other cases where you would not.I would not be surprised if that is wrong/missing something, but that is where I would start and maybe others can speak to why that would/wouldn’t be a good idea.
vrod
Thank you all for the suggestions! @eksperimental this source code was very helpful and @srowley you are right some decisions must be taken about this
:__struct__key.For example I tried implementing this with
DateTimestruct – I found this is almost 100% copy of the source code forMapimpl. The only change I tried was in the count function but I understand this may be different depends on what you need to do:eksperimental
This one is wrong, as the result of size will include the
:__struct__keyvrod
Actually my case works even if I return
{:error, "no slice"}for this function so I think this means that whateverEnumfunctions are being used they are not requiring theslicefunction. I am not sure if thissizevariable is like the size of “chunk” for getting part of a list? I think maybe sometimes you might want this to include__struct__key but It most probably depends on the use case. Anyhow I have now at least something that I can play around with. Thank you again!al2o3cr
I don’t understand this part - if you’re passing a struct where the third-party code expects an Enumerable, why couldn’t you just… not do that?
eksperimental
Nope. That is not how it works. It is explained in the documentation of each funtion.
eksperimental
Exactly, that’s why I wonder why would you want to implement the Enumerable for DateTime when you are just gonna treat it like a map. You’ d better off convert it to a map.