shahryarjb

shahryarjb

How to alias a module in __using__

Hello, I want to load a alias under my __using__, but with opts help like this:

defmacro __using__(opts) do
  quote(bind_quoted: [opts: opts]) do
    behaviour = Keyword.get(opts, :behaviour)
    alias behaviour
  end
end

It shows me invalid argument for alias, expected a compile time atom or alias, got: behaviour error, if I use alias unquote(behaviour), it shows me unquote called outside quote.

It should be noted, for @behaviour it works if I do like this, @behaviour behaviour but for alias doesn’t work.
How can I fix this? Thanks

Most Liked

LostKobrakai

LostKobrakai

One thing people usually have a hard time to grok is that macros only deal with AST – or “code”. Even if things look like a variable they don’t really represent the value assigned to the variable.

defmacro __using__(opts) do
  quote(bind_quoted: [opts: opts]) do
    behaviour = Keyword.get(opts, :behaviour)
    alias behaviour
  end
end

Doing this means alias (also a macro) will receive the AST for the behaviour variable (something like this: {:behaviour, [], Elixir}). So it has no idea what module it’s supposed to alias.

defmacro __using__(opts_ast) do
  behaviour = select_module_from_opts(opts_ast)
  quote do
    alias unquote(behaviour)
  end
end

This on the other hand pulls out the module out of the AST your macro received and creates AST (again a representation of code), where alias receives the module as an argument instead of a variable.

The tricky part here is select_module_from_opts, because again opts is AST not necessarily a plain value to consume. Some values represent themselves in AST – no difference between the AST and the value represented – so they’re rather easy to deal with (e.g. many keyword lists with simple values) , but others are not as simple, e.g. maps or anything containing other variables.

LostKobrakai

LostKobrakai

You‘ll need to pull out the module in the macro body and not in the quoted return. It needs to look like this:

alias unquote(module)

Anything where the alias macro doesn‘t receive the module name directly, but e.g. the ast of a variable won‘t work.

shahryarjb

shahryarjb

Thank you for your complete explanation. The subject is beyond my comprehension, and I’ve refrained from using alias. :rose:

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
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
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
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
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement