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
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.
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.







