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?

anders

anders

So in general, avoid using the same name fragments for different parts of the module name?

Where Next?

Popular in Questions Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement