Today I Learned

A Hashrocket project

Aliasing an Elixir Module Within Itself

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
Looking for help? Elixir is quickly gaining momentum for web applications that need concurrency, performance, and the ability to connect to many different clients. The developers at Hashrocket are learning along with the rest of the development community that Elixir and Phoenix are viable Rails alternatives for the right application. Check out the source code for Today I Learned, written in Elixir, and contact us if you need help with your Elixir project.