mguimas
What does %Mod{} really do?
Hello,
given the files xx.ex and yy.ex defined as
defmodule XX do
defstruct [:field]
end
defmodule YY do
defdelegate __struct__, to: XX
defdelegate __struct__(x), to: XX
end
why does
iex(1)> %XX{} == XX.__struct__()
true
and
iex(2)> %YY{} == YY.__struct__()
false
give different results?
It seems that %Mod{} is not equivalent to Mod.__struct__() …
What does %Mod{} really do?
Thanks
Mário
Most Liked Responses
peerreynders
iex(14)> Map.to_list(%YY{})
[__struct__: YY, field: nil]
iex(15)> Map.to_list(YY.__struct__())
[__struct__: XX, field: nil]
Based on the evidence I suspect that the compiled code (it’s a special form after all) sanitizes the value returned from the function by forcing the __struct__ value to reflect the primary module - but that is just a guess.
peerreynders
iex(16)> struct!(YY,field: 1)
%XX{field: 1}
iex(17)> struct!(%YY{},field: 1)
%YY{field: 1}
I suspect the bug is with Kernel.struct!/2, not Kernel.SpecialForms.%/2.
https://github.com/elixir-lang/elixir/blob/v1.8.1/lib/elixir/lib/kernel.ex#L2116-L2130
As you discovered, it only presents when you start defdelegating the constructor and I don’t think this code anticipated that.
So the question is - what are you trying to accomplish by defdelegating the struct constructor to another struct? Sharing code between XX and YY should probably be accomplished via a shared third module.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #podcasts
- #code-sync
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








