mattfara50

mattfara50

Is the default operator a macro?

HexDoc on default args

This states that the compiler creates new functions given the default operator. Is this actually a macro?

Marked As Solved

Eiji

Eiji

No, look that you cannot use \\ in function body. Elixir is capable of parsing a predefined set of operators. Some of them are macros, but some of them are used only within other macros (like defor defp) and because of that their AST representation is used instead.

(…) these operators appear in the precedence table above but are only meaningful within certain constructs:

Source: Operators reference — Elixir v1.20.2

Take a look at a simplest example:

defmodule Example do
  defmacro sample({:\\, _, [left, right]}) do
    quote bind_quoted: [left: left, right: right] do
      if is_nil(left) do
        {:right, right}
      else
        {:left, left}
      end
    end
  end
end

iex> require Example
Example
iex> Example.sample(1 \\ 2)
{:left, 1}
iex> Example.sample(nil \\ 2)
{:right, 2}

Since \\ is a valid operator you are also able to define it, so it can be used inside function body:

defmodule Example do
  def left \\ right do
    {left, right}
  end
end

iex> import Example
Example
iex> 1 \\ 2  
{1, 2}

Also Liked

al2o3cr

al2o3cr

\\ is parsed as an operator (like + or == or -> etc) but not normally defined anywhere:

iex(1)> 4 \\ 5
** (CompileError) iex:1: undefined function \\/2 (there is no such import)

iex(1)> quote do
...(1)>   4 \\ 5
...(1)> end
{:\\, [], [4, 5]}

The implementation of def transforms the AST that’s passed to it with pattern matching, but no function named \\ ever runs:

https://github.com/elixir-lang/elixir/blob/da409d8692319dc77b3516fccb84a50fa95a8477/lib/elixir/src/elixir_def.erl#L261-L297

Last Post!

mattfara50

mattfara50

Thank you. I have a lot to learn.

Where Next?

Popular in Questions Top

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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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

Other popular topics Top

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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
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

We're in Beta

About us Mission Statement