alias, alias, alias. Alias multiple module with same parent more easily

696 views
Skip to first unread message

Paul Smith

unread,
Mar 20, 2015, 11:35:32 AM3/20/15
to elixir-l...@googlegroups.com

Sometimes aliases get out of hand and either need to be extracted (not always ideal) or take up lots of space and are somewhat tedious to write and read through.

The example below is a common scenario when writing a Phoenix app. Would some kind of syntax like this be helpful?

defmodule
  # Same as writing
  # alias MyApp.Post
  # alias MyApp.User
  # alias MyApp.Comment
  # alias MyApp.Repo
  alias MyApp, [Post, User, Comment, Repo]
end

José Valim

unread,
Mar 20, 2015, 11:49:16 AM3/20/15
to elixir-l...@googlegroups.com
I remember this discussion appeared in the past and one of the proposals was to use {...} as delimiter:

    alias MyApp.{Post, User, Comment, Repo}

I can't remember though which language was mentioned (Python or Java?) that provides similar functionality.



José Valim
Skype: jv.ptec
Founder and Lead Developer

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/cf261314-28e4-4847-be9c-12cb62cc388a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Joe Honzawa

unread,
Mar 20, 2015, 6:00:41 PM3/20/15
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Awesome proposing. I have wanted this feature since before.

+1 to José
's idea.

Such notation is familiar to us, in shell.
$ mkdir dir{1,2,3}


> which language was mentioned (Python or Java?) that provides similar functionality.

It is similar to scala's import

import java.util.{ List => JavaList, Map => JavaMap }



2015年3月21日土曜日 0時49分16秒 UTC+9 José Valim:

Andrea Leopardi

unread,
Mar 21, 2015, 6:05:33 AM3/21/15
to elixir-l...@googlegroups.com
I'm extremely +1 on this, since it's a feature I often desired myself.

As for the syntax, I think the one proposed by Paul (alias MyApp, [Post, User, Comment, Repo]) is the cleanest one, the easiest to remember and probably the more "programmable" too (the array can be built up programmatically). 

José Valim

unread,
Mar 21, 2015, 7:02:27 AM3/21/15
to elixir-l...@googlegroups.com

I'm extremely +1 on this, since it's a feature I often desired myself.

As for the syntax, I think the one proposed by Paul (alias MyApp, [Post, User, Comment, Repo]) is the cleanest one, the easiest to remember and probably the more "programmable" too (the array can be built up programmatically). 

Because alias is lexical, it requires all the arguments to be given explicitly at compile time. So you can't do this:

foo = [Post, User]
bar = [Comment, Repo]
alias MyApp, foo ++ bar

If you want to do it somewhat dynamically, you'd need a macro, and then we are talking about doing:

alias MyApp, unquote(aliases)

Or:

alias MyApp.{unquote_splicing(aliases)}

Which are pretty much the same in terms of complexity. If you are using macros, you need to learn about splicing anyway, if you ever want to generate tuples, lists and function arguments dynamically.

So I would say both are equally programmable. :)


Andrea Leopardi

unread,
Mar 21, 2015, 7:11:03 AM3/21/15
to elixir-l...@googlegroups.com
Ah, didn't think about it. Nice!

I still think using a list may be more readable, but these are low-level details, what I want is this feature no matter how :).


Andrea Leopardi

--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/bAHpNoiDXK8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4KFhdLn7AhynU6ba9CzTRhoA1ahNh6A1rMV83jyQn9DMA%40mail.gmail.com.

José Valim

unread,
Mar 21, 2015, 7:21:51 AM3/21/15
to elixir-l...@googlegroups.com
Yes, it is still arguable which one is better, I was just discussing that particular point view. :)

One thing I don't like about the list is that they are a big ambiguous with the existing options:

alias Foo, [as: Bar]
alias Foo, [Bar, Baz, Bat]

We also need to consider if we are adding the same to require and import too (require mainly). Today, we can actually do:

require Foo.Bar, as: true

And that is equivalent to:

require Foo.Bar, as: Bar

The "as: true" option is not super clear though. We may consider changing that but imagine someone should be able to do something like:

require Foo.{Bar, Baz}, as: true # alias and requires in one step

or:

require Foo, [Bar, Baz], as: true

We need to consider all those scenarios.



José Valim
Skype: jv.ptec
Founder and Lead Developer

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAM9Rf%2BLbTogUeS1bkoE7gmwbYy6nhGMoXBPEoB0w1kPWdCt%3DRA%40mail.gmail.com.

Paul Smith

unread,
Mar 31, 2015, 2:22:34 PM3/31/15
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Thoughts on this? I think this would resolve your concerns, but I'm not sure. 

alias [Post, User, Comment, Repo], from: MyApp

If we don't like this syntax, I think `alias MyApp{Post, User, Comment, Repo} makes the most sense.

I might be able to work on adding this once we decide what we want, but I might need some guidance on how to do so.

José Valim

unread,
Mar 31, 2015, 3:05:11 PM3/31/15
to elixir-l...@googlegroups.com
Thanks Paul! I will need to write a proposal and consider how require and import will be affected too. It won't happen soon though, I will ping back here once I do. hopefully you will be able to use the proposal as a guide for your implementation 
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/b4c37b2f-7fc9-4a2c-8b9d-3efc38be6f20%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--

Θήρων Börner

unread,
Aug 29, 2015, 2:01:28 PM8/29/15
to elixir-lang-core, jose....@plataformatec.com.br
José, have you made any progress on a proposal?
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.

José Valim

unread,
Sep 1, 2015, 7:54:02 AM9/1/15
to Θήρων Börner, elixir-lang-core
A proposal considering the different directives, macros, as well as parser changes and the required validation has been written: https://github.com/elixir-lang/elixir/issues/3646



José Valim
Skype: jv.ptec
Founder and Director of R&D

To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

Aleksei Magusev

unread,
Sep 25, 2015, 8:09:39 AM9/25/15
to elixir-lang-core, hunter...@gmail.com, jose....@plataformatec.com.br
The support for it has just recently been merger into master: https://github.com/elixir-lang/elixir/pull/3666.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.

moloney...@gmail.com

unread,
Mar 8, 2016, 6:02:40 AM3/8/16
to elixir-lang-core, hunter...@gmail.com, jose....@plataformatec.com.br
1 level deep multi alias: (works)
alias MyApp.{Repo, UserManager.User, UserManager.Mail}

2 level deep multi alias (nested multi alias): (not supported)
alias MyApp.{Repo, UserManager.{User, Mail}}
invalid argument for alias, expected a compile time atom or alias, got: UserManager.{}(User, Mail)

Any thoughts on support for nested multi alias' ?

José Valim

unread,
Mar 8, 2016, 6:27:04 AM3/8/16
to moloney...@gmail.com, elixir-lang-core, hunter...@gmail.com
You should break that into two aliases definitions. There is no plans for supporting nesting because I would consider that approach quite unreadable. :)
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.


--


José Valim
Skype: jv.ptec
Founder and Lead Developer




--


José Valim
Skype: jv.ptec
Founder and Director of R&D

moloney...@gmail.com

unread,
Mar 8, 2016, 6:32:22 AM3/8/16
to elixir-lang-core, moloney...@gmail.com, hunter...@gmail.com, jose....@plataformatec.com.br
Yes, makes sense. Clearer with two aliases alright.

alias MyApp.Repo
alias MyApp.UserManager.{User, Mail}

thanks.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.


--


José Valim
Skype: jv.ptec
Founder and Lead Developer

Reply all
Reply to author
Forward
0 new messages