mcgingras

mcgingras

Hello all,

I’ve been reading about metaprogramming and encountered examples that look like the following:
defmodule example(example_param, do: block)

I haven’t encountered syntax with the do param like that before, and I’ve been confused how that works. I have not been able to find much in the way of documentation since quite honestly I’m not even sure what to search for.

How does the caller pass the “block” to the function? What is this sort of syntax called?

Showing Posts 1 to 3

RudManusachi

RudManusachi

It’s nothing but the keyword list with key :do. The equivalent would be:
defmodule example(example_param, [{:do, block}])

And usually inside the macro you unquote the block.

if the last argument of a call is a keyword list then the square brackets can be skipped

See keywords as last arguments, followed by the section about do/end blocks

The other thing is elixir’s tokenizer treats do/end as an “expression” where end is a terminal “end of expression”.

Eiji

Eiji

@mcgingras If you do not understand some syntax you should check Syntax reference special documentation page.

Regarding your questions you should be especially interested in those sections:

Syntactic sugar

All of the constructs above are part of Elixir’s syntax and have their own representation as part of the Elixir AST. This section will discuss the remaining constructs that “desugar” to one of the constructs explored above. In other words, the constructs below can be represented in more than one way in your Elixir code and retain AST equivalence.

Source: Syntax reference — Elixir v1.20.2

Keywords

Keywords in Elixir are a list of tuples of two elements where the first element is an atom. Using the base constructs, they would be represented as:

[{:foo, 1}, {:bar, 2}]

However Elixir introduces a syntax sugar where the keywords above may be written as follows:

[foo: 1, bar: 2]

Source: Syntax reference — Elixir v1.20.2

Keywords as last arguments

Elixir also supports a syntax where if the last argument of a call is a keyword list then the square brackets can be skipped. This means that the following:

if(condition, do: this, else: that)

is the same as

if(condition, [do: this, else: that])

which in turn is the same as

if(condition, [{:do, this}, {:else, that}])

Source: Syntax reference — Elixir v1.20.2

do/end blocks

The last syntax convenience are do/end blocks. do/end blocks are equivalent to keywords as the last argument of a function call where the block contents are wrapped in parentheses. For example:

if true do
  this
else
  that
end

is the same as:

if(true, do: (this), else: (that))

Source: Syntax reference — Elixir v1.20.2

mcgingras

mcgingras OP

Thank you! I wasn’t sure what to search within the reference material. “Keywords as last arguments” is exactly what I am looking for. Thanks so much for the help.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews