Hi
The help here https://elixir-lang.org/blog/2014/06/17/elixir-v0-14-0-released/ suggests that
defmodule User do
@derive [Enumerable]
defstruct name: "", age: 0
end
Derives Enumerable for a Struct.
Every thread I’ve read reverts to using Map.from_struct/1. Indeed, that’s what I always do and am doing again just to keep my code working.
When I use @derive I get this error.
could not load module Enumerable.Any due to reason :nofile, cannot derive Enumerable for [Module]
Just thought I’d ask if anyone could point to an example of deriving Enumerable for a Struct. If it can’t be done with a single line of code I’ll keep using Map.from_struct/1.
Deriving requires a protocol implementation for Any
, which doesn‘t exist for Enumerable
. The source you linked is from elixir 0.14, which is both very old and pre 1.0. Such information cannot be trusted to still be valid today.
1 Like
Yeah, I appreciated that was for an old version of Elixir.
Therefore, is it to be concluded, for anyone happening on this thread, that:
a. the most common practice is Map.from_struct/1 and
b. @derive doesn’t work w/o lots of extra effort and
c. that there’s no example explaining specifically how to derive Enumerable for Structs
Just asking.
I’m happy to continue doing option a. 
Yes there‘s no way of deriving enumerable.
However you can still implement the Enumerable protocol for structs. If you just want to make the struct enumerate like the underlying map, you can even delegate to the map implementation. However there‘s no expectation that you want to enumerate over data like that, hence there being no shortcuts. E.g. take a look at MapSet, which enumerates completely different to the actual internals of the struct.
1 Like
Super, I think that clears that up and will be useful for anyone else happening along this road