mguimas
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
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
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
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #iex
- #elixirconf-us
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peerreynders
Kernel.SpecialForms.%/2Based 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.mguimas
My question is this behavior some kind of internal “black magic”, as you suggest, or a bug …
Isn’t
%Mod{}supposed to be the same as callingMod.__struct__()?mguimas
I kind of suspect this might be a bug, as per this evidence:
this seems ok
this seems wrong
Does anyone have an explanation for this, or is this a bug?
If
%Mod{}is supposed to be the same as callingMod.__struct__()orstruct!(Mod), then it is a bug.Thanks
peerreynders
I doubt it’s a bug. For example the Access Behavior makes use of that
__struct__value to access the structure specific implementations for navigation and access. If__struct__was left atXXyou would automatically “inherit” all theXXfunctionality but have no way of overriding it forYY.mguimas
But then the behavior of
struct!(YY, ...)should also be to return an%YY{...}, but it is returning an%XX{...}…There is some inconsistency somewhere, hence a bug.
peerreynders
I suspect the bug is with
Kernel.struct!/2, notKernel.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
structconstructor to anotherstruct? Sharing code betweenXXandYYshould probably be accomplished via a shared third module.mguimas
This issue has been solved here.