mattfara50

mattfara50

HexDoc on default args

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

Showing Posts 1 to 3

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

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}
mattfara50

mattfara50 OP

Thank you. I have a lot to learn.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews