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.16.0

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:

Where Next?

Popular in Questions Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Tee
can someone please explain to me how Enum.reduce works with maps
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
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
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement