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

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현