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;








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










share|improve this question






























    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










    share|improve this question


























      0












      0








      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










      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      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






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer
























            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
            );



            );













            draft saved

            draft discarded


















            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









            0














            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.






            share|improve this answer





























              0














              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.






              share|improve this answer



























                0












                0








                0







                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.






                share|improve this answer















                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                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


















                    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.



















                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript