Today I Learned: Aliasing an Elixir Module Within Itself
Wednesday August 1, 2018 at 09:21 pm CST
originally published 1/26/2018 on the Hashrocket TIL blog
I was attempting to compile a Phoenix application and I got this error:
Post.__struct__/0 is undefined, cannot expand struct Post
The issue was in a function I defined in the module.
def changeset(%Post{}= post, attrs \\ %{}) do
...
end
I assumed that you would get references to a module within said module for free. That’s not the case. There are two ways to fix the error. One is to use the full module name in the parameter list.
def changeset(%Forum.Post{}= post, attrs \\ %{}) do
...
end
Alternatively, I can alias the module within itself.
alias Forum.Post
Photo by Jonny Caspari on Unsplash