anders

anders

Using aliases for Fubar.Fubar named module?

I’m playing around with something like this:

defmodule Todo.User do
  defstruct name: nil
end

defmodule Todo.Todo do
  defstruct text: nil
  def complete(%Todo.Todo{} = todo, %Todo.User{} = completer), do: nil
end

Now, writing %Todo.Todo{} and %Todo.User{} is tedious. Especially if you have a lot of references to the types from a lot of functions. I would prefer %Todo{} and %User{}.

What is the best way of doing this? Is there any good reason not to look for a shorthand here?

I tried this:

defmodule Todo.Todo do
  alias Todo.User
  alias Todo.Todo
  defstruct text: nil
  def complete(%Todo{} = todo, %User{} = completer), do: nil
end

and it works, but now the order of the alias:es is important and I have to alias everything with the Todo prefix. Doesn’t feel right.

Marked As Solved

josevalim

josevalim

Creator of Elixir

If you want to alias them, then surely. Otherwise you will run into the conflicts you described.

Also Liked

gregvaughn

gregvaughn

The module aliases itself? Interesting. I’ve never seen that before. What’s more common is to use __MODULE__ as a compile time reference to the current module.

anders

anders

I’ve seen that in GenServers, but how would that translate to this case?

defmodule Todo.Todo do
  alias Todo.User
  # alias Todo.Todo
  defstruct text: nil
  def complete(%__MODULE__{} = todo, %User{} = completer), do: nil
end

That just looks wierd to me. But I’m new to Elixir, perhaps my eyes haven’t adjusted yet :slight_smile:

josevalim

josevalim

Creator of Elixir

If the naming schema is confusing, I can’t think of a better option besides changing the name. Why not call it Todo.List for the whole list and Todo.ListItem or similar for every element?

Where Next?

Popular in Questions Top

WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31194 112
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement