verilog

verilog

Macros - how are they different to functions? When should they be used? How do they differ to C's macros?

Hello, I’m new to Elixir.
I was reading about the macros of Elixir but didn’t quite get it. I know C and familiar with its marcos.
Although I didn’t quite understand Elixir’s macros yet. Could you answer my basic questions about macros:

  1. What is the difference between functions and macros in Elixir?
  2. Why or when Should I use them?
  3. How they are different for C’s macros?

Most Liked

stefanchrobot

stefanchrobot

Re 1. Given:

my_thing(:some_param)

If my_thing is a macro, the line above will be evaluated at compilation time, otherwise it will be evaluated at run-time. Note that macro can invoke a function which means the function will be invoked at compilation time, which in turn means you can do all kinds of fancy and crazy stuff at compile-time.

Re 2. Use macros if functions are not enough to remove boilerplate. The line is blurry. Generally, avoid using macros.

Re 3. Elixir macros are safer (hygienic) and more powerful. They transform a piece of Elixir code into a different piece of Elixir code. The coolest thing is that the piece of code you’re working with is expressed as an Elixir data structure.

I highly recommend going through the blog series about macros by Saša Jurić.

NobbZ

NobbZ

C Macros are very similar to the “search and replace” function in your editor, as that is exactly how they are implemented. They are very dumb.

Elixir macros though are able to “understand” the arguments they got and produce completely different code for different arguments.

OvermindDL1

OvermindDL1

C Macro’s are indeed a templating engine, more similar to EEX than anything else, entirely textual and not AST aware at all. Elixir Macro’s are AST transformers, more like LISP macro’s, which is closer to C++ templates. Essentially an Elixir Macro is a function that executes at compile time to transform one AST form (whatever is passed in to it as an AST instead of as values) and returns a new AST to put in it’s place, and that continues resolving down until it is finally at the ‘base’ AST level and no more expansions can happen, which is then when it is compiled into Erlang.

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Other popular topics 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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
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
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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 54120 245
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement