mattfara50

mattfara50

HexDoc on default args

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

First 3 of 3 Posts Switch mode

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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement