phykos

phykos

Blocks -- Is there any way to define a function that does take a do .. end block?

In Ruby, which is very similar to Elixir I do this:

def test
 yield
end

test do
 puts("sup there")
end

Here, the yield keyword will be replaced by the statements (in this case, puts("sup there"), but inside it I can put pretty much whatever I want) inside the block.

How to do this in Elixir?

Most Liked

al2o3cr

al2o3cr

It works, but the evaluation order won’t entirely match the “Ruby blocks” intuition, especially when things like else get involved:

defmodule DoTest do
  def foo(do: x, else: y) do
    IO.inspect({x, y}, label: "in foo")
  end
end

DoTest.foo do
  IO.puts("in do")
else
  IO.puts("in else")
end

## prints:
# in do
# in else
# in foo: {:ok, :ok}

The parser makes the transformation from do ... end to do: ... consistently, but plain def always evaluates arguments (versus defmacro which passes arguments as AST)

lpil

lpil

Creator of Gleam

Welcome to the forum!

It’s not possible to define a function that does this. You can define a macro that does this, but the behaviour is extremely different to the behaviour in Ruby.

To achieve what you’ve described you would use an anonymous function:

test fn ->
  puts("sup there")
end

This is conceptually the same as a block/proc in Ruby.

baldwindavid

baldwindavid

Perhaps this also approximates what you are looking for?

def test(do: block)
 block
end

test do
 puts("sup there")
end
phykos

phykos

thanks man!

baldwindavid

baldwindavid

@NobbZ I’ve been using that in my codebase and it seems to work fine. I use it in functions that create an easy DSL for html emails.

Inside a module my example above can be run in iex. Is it perhaps working, but something I should avoid?

Where Next?

Popular in Chat/Questions Top

InkFlo
Hi everyone, This year I’m graduated from Bachelor Degree (in computer science) from France (not really a bachelor, the exact term is “L...
New
xgilarb
Hi there, I’m interested in using Elixir because of the rumors about the reliability of the Phoenix framework, and surprisingly, Elixir’...
New
g5becks
Hello everyone. So I just got done reading all of the introduction to Elixir and working through the basics of the language. I am not new...
New
sadcad
I love the Phoenix and Elixir docs, but I always tend to learn faster when I watch a video of someone explaining things and then I implem...
New
Scoty
Hey, I am currently reading Programming Elixir and I am doing one of the exercises where you should write a solution to the “I’m thinkin...
New
ggwc82
Looking to get started with FP and Elixir coming mainly from an OOP Rails and PHP background. My first question is, whats the best cours...
New
woohaaha
I’m coming from Ruby and Rails. I have read some Elixir and Phoenix books. They shed a lot of light about building applications in Elixir...
New
Besto
I've been trying to start learning Elixir for a couple of weeks while I develop a tiny project I have on node.js, but every time I try to...
New
asfand
Hi Everyone, I am a student and know basics of web development, used some php and ruby, but I am not an expert in any. I want to learn E...
New
Allyedge
So, I want to get an Elixir book, but don’t know which one to get. Both Programming Elixir 1.6 and Elixir in Action looks interesting, b...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement