While macro bind_quoted breaks on first iterationHow to create a dynamic function name using Elixir macro?Elixir - Calling / Invoking Macros - UndefinedFunctionErrorWriting a custom assertion macroDynamic function definition with macroShould tuples be passed to macro?Elixir macros, quoting and unquotingUnquoting argument in macro definition hangs function callElixir: application quits immediately, or doesn't receive input when running distillery package. Why does it work that way, and how to work around it?Elixir: required inside a macroIO.inspect inside elixir Macros
Does a reference have a storage location?
Mixing Acrylic Paint With Water ( And Storing It )
How frequently do Russian people still refer to others by their patronymic (отчество)?
Why are symbols not written in words?
What can a novel do that film and TV cannot?
Who are the police in Hong Kong?
Is my background sufficient to start Quantum Computing
Can the word "coexist" be used for more than two things/people/subjects/... etc?
Performance of loop vs expansion
Isn't "Dave's protocol" good if only the database, and not the code, is leaked?
A student "completes" 2-week project in 3 hours and lies about doing it himself
What does "another" mean in this case?
Can you use a reaction to affect initiative rolls?
How might boat designs change in order to allow them to be pulled by dragons?
Story about two rival crews terraforming a planet
What do you call the angle of the direction of an airplane?
Magento 2: I am not aware about magneto optimization. Can you please share the steps for this?
What is -(-2,3,4)?
How can I get a file's size with C++17?
What instances can be solved today by modern solvers (pure LP)?
Should I cross-validate metrics that were not optimised?
How can solar sailed ships be protected from space debris?
gzip compress a local folder and extract it to remote server
How did sloshing prevent the Apollo Service Module from moving safely away from the Command Module and how was this fixed?
While macro bind_quoted breaks on first iteration
How to create a dynamic function name using Elixir macro?Elixir - Calling / Invoking Macros - UndefinedFunctionErrorWriting a custom assertion macroDynamic function definition with macroShould tuples be passed to macro?Elixir macros, quoting and unquotingUnquoting argument in macro definition hangs function callElixir: application quits immediately, or doesn't receive input when running distillery package. Why does it work that way, and how to work around it?Elixir: required inside a macroIO.inspect inside elixir Macros
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Maybe I am misunderstanding the use of bind_quoted but take this simple while loop macro:
defmodule Loop do
defmacro while(expression, do: block) do
quote bind_quoted: [expression: expression, block: block] do
for _ <- Stream.cycle([:ok]) do
if(expression) do
block
end
end
end
end
end
Usage:
Interactive Elixir (1.8.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c "loop.ex"
[Loop]
iex(2)> import Loop
iex(3)> while 1 == 1 do
...(3)> IO.puts "hi"
...(3)> end
hi
I would expect an infinite loop of "hi" but instead only get one iteration. If I remove the bind_quoted and simple unquote() each argument, it works as expected. Any ideas?
Thanks
elixir
add a comment |
Maybe I am misunderstanding the use of bind_quoted but take this simple while loop macro:
defmodule Loop do
defmacro while(expression, do: block) do
quote bind_quoted: [expression: expression, block: block] do
for _ <- Stream.cycle([:ok]) do
if(expression) do
block
end
end
end
end
end
Usage:
Interactive Elixir (1.8.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c "loop.ex"
[Loop]
iex(2)> import Loop
iex(3)> while 1 == 1 do
...(3)> IO.puts "hi"
...(3)> end
hi
I would expect an infinite loop of "hi" but instead only get one iteration. If I remove the bind_quoted and simple unquote() each argument, it works as expected. Any ideas?
Thanks
elixir
add a comment |
Maybe I am misunderstanding the use of bind_quoted but take this simple while loop macro:
defmodule Loop do
defmacro while(expression, do: block) do
quote bind_quoted: [expression: expression, block: block] do
for _ <- Stream.cycle([:ok]) do
if(expression) do
block
end
end
end
end
end
Usage:
Interactive Elixir (1.8.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c "loop.ex"
[Loop]
iex(2)> import Loop
iex(3)> while 1 == 1 do
...(3)> IO.puts "hi"
...(3)> end
hi
I would expect an infinite loop of "hi" but instead only get one iteration. If I remove the bind_quoted and simple unquote() each argument, it works as expected. Any ideas?
Thanks
elixir
Maybe I am misunderstanding the use of bind_quoted but take this simple while loop macro:
defmodule Loop do
defmacro while(expression, do: block) do
quote bind_quoted: [expression: expression, block: block] do
for _ <- Stream.cycle([:ok]) do
if(expression) do
block
end
end
end
end
end
Usage:
Interactive Elixir (1.8.0) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> c "loop.ex"
[Loop]
iex(2)> import Loop
iex(3)> while 1 == 1 do
...(3)> IO.puts "hi"
...(3)> end
hi
I would expect an infinite loop of "hi" but instead only get one iteration. If I remove the bind_quoted and simple unquote() each argument, it works as expected. Any ideas?
Thanks
elixir
elixir
edited Mar 25 at 19:15
Brett Beatty
1,8111 gold badge14 silver badges24 bronze badges
1,8111 gold badge14 silver badges24 bronze badges
asked Mar 25 at 18:09
BotonomousBotonomous
1,2291 gold badge11 silver badges33 bronze badges
1,2291 gold badge11 silver badges33 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
When you use bind_quoted, it evaluates the pieces you pass in. So the IO.puts "hi"
is evaluated in [expression: expression, block: block]
, not inside your if
block. You're just injecting an :ok
there.
As an example, let's define some macros that do nothing except evaluate what's passed in:
defmodule AB do
defmacro a(block) do
quote bind_quoted: [block: block] do
block
block
:ok
end
end
defmacro b(block) do
quote do
unquote(block)
unquote(block)
:ok
end
end
end
Ignoring some warnings from the first one, let's call each of these with a block containing IO.inspect/1
and see what happens:
iex> require AB
iex> AB.a(IO.inspect(1 + 1))
... (some warnings)
2
:ok
iex> AB.b(IO.inspect(1 + 1))
2
2
:ok
In macro a/1
, the sum + inspect happens outside of the quoting, so it only gets called the once. In macro b/1
, the sum + inspect happens anywhere we unquote block
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344067%2fwhile-macro-bind-quoted-breaks-on-first-iteration%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you use bind_quoted, it evaluates the pieces you pass in. So the IO.puts "hi"
is evaluated in [expression: expression, block: block]
, not inside your if
block. You're just injecting an :ok
there.
As an example, let's define some macros that do nothing except evaluate what's passed in:
defmodule AB do
defmacro a(block) do
quote bind_quoted: [block: block] do
block
block
:ok
end
end
defmacro b(block) do
quote do
unquote(block)
unquote(block)
:ok
end
end
end
Ignoring some warnings from the first one, let's call each of these with a block containing IO.inspect/1
and see what happens:
iex> require AB
iex> AB.a(IO.inspect(1 + 1))
... (some warnings)
2
:ok
iex> AB.b(IO.inspect(1 + 1))
2
2
:ok
In macro a/1
, the sum + inspect happens outside of the quoting, so it only gets called the once. In macro b/1
, the sum + inspect happens anywhere we unquote block
.
add a comment |
When you use bind_quoted, it evaluates the pieces you pass in. So the IO.puts "hi"
is evaluated in [expression: expression, block: block]
, not inside your if
block. You're just injecting an :ok
there.
As an example, let's define some macros that do nothing except evaluate what's passed in:
defmodule AB do
defmacro a(block) do
quote bind_quoted: [block: block] do
block
block
:ok
end
end
defmacro b(block) do
quote do
unquote(block)
unquote(block)
:ok
end
end
end
Ignoring some warnings from the first one, let's call each of these with a block containing IO.inspect/1
and see what happens:
iex> require AB
iex> AB.a(IO.inspect(1 + 1))
... (some warnings)
2
:ok
iex> AB.b(IO.inspect(1 + 1))
2
2
:ok
In macro a/1
, the sum + inspect happens outside of the quoting, so it only gets called the once. In macro b/1
, the sum + inspect happens anywhere we unquote block
.
add a comment |
When you use bind_quoted, it evaluates the pieces you pass in. So the IO.puts "hi"
is evaluated in [expression: expression, block: block]
, not inside your if
block. You're just injecting an :ok
there.
As an example, let's define some macros that do nothing except evaluate what's passed in:
defmodule AB do
defmacro a(block) do
quote bind_quoted: [block: block] do
block
block
:ok
end
end
defmacro b(block) do
quote do
unquote(block)
unquote(block)
:ok
end
end
end
Ignoring some warnings from the first one, let's call each of these with a block containing IO.inspect/1
and see what happens:
iex> require AB
iex> AB.a(IO.inspect(1 + 1))
... (some warnings)
2
:ok
iex> AB.b(IO.inspect(1 + 1))
2
2
:ok
In macro a/1
, the sum + inspect happens outside of the quoting, so it only gets called the once. In macro b/1
, the sum + inspect happens anywhere we unquote block
.
When you use bind_quoted, it evaluates the pieces you pass in. So the IO.puts "hi"
is evaluated in [expression: expression, block: block]
, not inside your if
block. You're just injecting an :ok
there.
As an example, let's define some macros that do nothing except evaluate what's passed in:
defmodule AB do
defmacro a(block) do
quote bind_quoted: [block: block] do
block
block
:ok
end
end
defmacro b(block) do
quote do
unquote(block)
unquote(block)
:ok
end
end
end
Ignoring some warnings from the first one, let's call each of these with a block containing IO.inspect/1
and see what happens:
iex> require AB
iex> AB.a(IO.inspect(1 + 1))
... (some warnings)
2
:ok
iex> AB.b(IO.inspect(1 + 1))
2
2
:ok
In macro a/1
, the sum + inspect happens outside of the quoting, so it only gets called the once. In macro b/1
, the sum + inspect happens anywhere we unquote block
.
edited Mar 25 at 23:47
answered Mar 25 at 19:29
Brett BeattyBrett Beatty
1,8111 gold badge14 silver badges24 bronze badges
1,8111 gold badge14 silver badges24 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344067%2fwhile-macro-bind-quoted-breaks-on-first-iteration%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown