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

Where Next?

Popular in Chat/Questions Top

ariandanim
Hello all, I am still learning Elixir, then go into Phoenix, i am try search in google but find the programming phoenix 1.4, another for...
New
ericdouglas
I think that would be really interesting to have official books created by the community about all kinds of development we can do with El...
New
nur
https://e.planaria.network Build a NoSQL DB, Build a Relational SQL Database, Build a Graph Database, Build a File System, Build a Bitc...
New
wolfiton
Hi everyone, How can i retrieve the name from a structure like this? %{"id" => "1570", "name" => "Croque Monsieur"} My test loo...
New
maz
I’m getting this error: ## ** (Ecto.Query.CompileError) Tuples can only be used in comparisons with literal tuples of the same s...
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
marciol
Hey, I have very restricted resources and time so I’m trying to understand the best way to learn Liveview in terms of cost/time. The Pra...
New
jsnoble
I come from a javascript background and I just starting learning elixir. I went through the basic tutorials but I want something deeper. ...
New
zeroexcuses
Besides https://elixir-lang.org/getting-started/basic-types.html are there any other well recommended “elixir by example” style resources...
New
jslearner
Will learning Erlang really help in being a better Phoenix or Elixir developer or is it a waste of time?
New

Other popular topics Top

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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement