Unbalanced Parentheses
Tags: erlang, compilers
Published: 2017-04-05 02:38:34

---
title: Unbalanced Parentheses
published: true
description: This is an article about something I showed live at the Erlang & Elixir Factory San Francisco 2017. It’s about what you can and can not do if you are fancy with your module attributes in Erlang.
cover_image:
tags: erlang, compilers
---

## …or another reason not to use macros

This is an article about something I showed *live* at the [Erlang & Elixir Factory San Francisco 2017](http://www.erlangelixir.com/brujo-benavides.html). It’s about what you can and can not do if you are fancy with your module attributes in Erlang.

![Golden Gate Park, from GrandView Park, SF](https://cdn-images-1.medium.com/max/2000/1*rcTFoMlMmoqJS3ZfU1-f7Q.jpeg)

### Notes

First of all, [Federico](https://medium.com/@unbalancedparen): sorry for stealing your name. It was just too cool not to ;)
That said, here is the video of the whole talk, in case you want to watch it…

<iframe width="560" height="315" src="https://www.youtube.com/embed/soKTK6hK70s" frameborder="0" allowfullscreen></iframe>

And if you prefer Spanish…

<iframe width="560" height="315" src="https://www.youtube.com/embed/DpjD_1e_MNI?list=PL4Vf1Ly5O7E6d5orXysur_sd_SFkbTV8U" frameborder="0" allowfullscreen></iframe>

Ok, enough with the links! Let’s get down to business…

*****

### The Usual Suspects

This is a perfectly valid Erlang module:

```erlang
    -module(my_mod).
    -export([whoami/0]).

    -spec whoami() -> atom().
    whoami() -> my_mod.
```

You can compile it and execute its function…

```erlang
    1> c(my_mod).
    {ok,my_mod}
    2> my_mod:whoami().
    my_mod
    3>
```

This is also a perfectly valid Erlang module (now using a macro):

```erlang
    -module(my_mod).
    -export([whoami/0]).

    -spec whoami() -> atom().
    whoami() -> ?MODULE.
```

As expected, same results…

```erlang
    1> c(my_mod).
    {ok,my_mod}
    2> my_mod:whoami().
    my_mod
    3>
```

*****

### The Cool Guy

But… that **spec** attribute there has no parentheses around it. Can we also remove the parentheses around the other attributes? The [docs](http://erlang.org/doc/reference_manual/modules.html#id77415) are not very clear about that. They show the following general syntax for attributes:

```erlang
-Tag(Value).
```

But then there are attributes like *spec*, *type* or *callback* that are shown without parentheses. So, why not give it a try? How much nicer would that export be without the parentheses around the brackets, right?

```erlang
    -module my_mod.
    -export [whoami/0].

    -spec whoami() -> atom().
    whoami() -> my_mod.
```

It actually works!! Look at that…

```erlang
    1> c(my_mod).
    {ok,my_mod}
    2> my_mod:whoami().
    my_mod
    3>
```

![YEY!! (from Primo GIF)](https://cdn-images-1.medium.com/max/1600/1*0NffgULjv2vhlMlhpc4xCg.gif)

I’m not writing a parentheses there anymore!

*****

### Not so Fast, Cowboy!

Well… I’m not a fan of macros, but sometimes I end up using the predefined ones, like I did with *?MODULE *above*…*

```erlang
    -module my_mod.
    -export [whoami/0].

    -spec whoami() -> atom().
    whoami() -> ?MODULE.
```

Now if you try to compile that module, you’ll find quite an unexpected result…

```erlang
    1> c(my_mod).
    my_mod.erl:5: 
    my_mod.erl:2: function whoami/0 undefined
    my_mod.erl:4: spec for undefined function whoami/0
    error
```

![WAT (from Primo GIF)](https://cdn-images-1.medium.com/max/1200/1*qOekxkFDrnQQIekBjkouiQ.gif)

*****

### What’s going on here?

This time I literally have no clue. I can see that, in order to use the macro *?MODULE* you have to use parentheses around your module name. But I haven’t done any research to find the root cause of it. That’s mostly because I have a very distinctive feeling that it will take me to the land of parsers, compilers, etc. and that’s a place I like to stay away from.

So, for this article, dear reader, if you are brave enough to go deep into the realm of the Erlang compiler and its friends and find out what’s going on, please let me know in the comments below.

And of course, if you can go ahead and fix this by sending a Pull Request to [erlang/otp](https://github.com/erlang/otp)…

![Office Space (1999)](https://cdn-images-1.medium.com/max/1600/1*cB1elrj2xoC7XFZZ_XnrVQ.jpeg)

*****

This article was first published in [*Erlang Battleground*](https://medium.com/erlang-battleground) as [**Unbalanced Parentheses**](https://medium.com/erlang-battleground/unbalanced-parentheses-6634140c6adb).