dokuzbir

dokuzbir

Need help about quote and unquote

This code is from metaprograming elixir book. I dont understand when should use unquote. In first function there is no unqote to execute expression but in second function there is unquote why ?

defmodule ControlFlow do
 # Why there is no unquote here ?
  defmacro my_if(expr, do: if_block), do: if(expr, do: if_block, else: nil) 
 
# Why there is unquote here ?
  defmacro my_if(expr, do: if_block, else: else_block) do
    quote do
      case unquote(expr) do
        result when result in [false, nil] -> unquote(else_block)
        _ -> unquote(if_block)
      end
    end
  end
end

Marked As Solved

aethereus

aethereus

Your no-quote version of my_if does not work. I tested it with the following code:

ControlFlow.my_if 1 > 2, do: IO.puts("This should not be printed.")

And I saw that the sentence “This should not be printed.” is actually printed. This is because when you pass 1 > 2 to expr, it gets automatically quoted (becomes AST), and the AST is {:>, [context: Elixir, import: Kernel], [1, 2]} and is not false or nil, so the test of if(expr, ...) passes, and the if_block (which is also an AST) gets returned by the macro my_if.

If you test it with

ControlFlow.my_if false, do: IO.puts("This should not be printed.")

You get the correct behavior by accident because false is an atom, and atoms are “Elixir literals” that have exactly the same form in code and in AST.

Also Liked

gregvaughn

gregvaughn

It’s calling Kernel.if/2 which is a macro that handles the quoting itself. But if you’re going to rely on Kernel.if then why write my_if? That’s why I think it’s a typo.

OvermindDL1

OvermindDL1

Should be:

quote(do: my_while(expression, do: block))

Also, your Stream.cycle bit is not ever going to end, it will loop forever?

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
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
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement