<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="281470" data-post-id="281470">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tmbb" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tmbb
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="D4no0" data-post="11" data-topic="54521">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/48/33624_2.png" class="avatar"> D4no0:</div>
<blockquote>
<p>this can be achieved with functions only</p>
</blockquote>
</aside>
<p>What is it that you say that can be achieved with functions only? The end result of <code>use</code>ing <code>CodeGen</code> is to add functions to the currenty module without the user having to spell them out (just like <code>use GenServer</code> does, for example). That has to be done with metaprogramming, AFAIK.</p>
<p>Are you saying that we should delegate to the template module instead of adding the function definitions to the user module?</p>
<p>In my case, having a component library was the use case that inspired this library. In such a library, you might have <code>fancier_input(assigns)</code> which depends on <code>input(assigns)</code>. If you delegate to another module, you’ll have something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defdelegate fancier_input(assigns), to: SomeLibraryModule
defdelegate input(assigns), to: SomeLibraryModule
</code></pre>
<p>This means that if you change <em>our own</em> implementation of <code>input(assigns)</code>, those changes will not be reflected on <code>fancier_input(assigns)</code>. And in this case I’d argue you want these changes to be reflected. The goal is not to be maximally functionaly pure, the goal is to have generators that work <em>on demand</em> by “generating” only the functions you want to customize.</p>
<aside class="quote no-group" data-username="D4no0" data-post="11" data-topic="54521">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/48/33624_2.png" class="avatar"> D4no0:</div>
<blockquote>
<p>if you really want to abstract function generation, you need to get rid of the macro syntax</p>
</blockquote>
</aside>
<p>By “macro syntax” you mean the quote block or the <code>CodeGen.block "name" do ... end</code> blocks inside the quote block?</p>
<aside class="quote no-group" data-username="D4no0" data-post="11" data-topic="54521">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/48/33624_2.png" class="avatar"> D4no0:</div>
<blockquote>
<p>I don’t have to think about the magic you are doing under the hood.</p>
</blockquote>
</aside>
<p>In case it’s not clear, I’m not doing much more magic than what a normal <code>use SomeModule</code> macro would do. <code>CodeGen</code> is basically a fancier version of the <code>use ...</code> macro which supports (optionally) adding some code to your file. Normal <code>use ...</code> invocations (such as the ones you find in Phoenix or Plug, for example), already perform a huge amount of magic behind the scenes to make things work.</p>
<aside class="quote no-group" data-username="D4no0" data-post="11" data-topic="54521">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/48/33624_2.png" class="avatar"> D4no0:</div>
<blockquote>
<p>otherwise I will just use classic metaprogramming as I already know how those concepts work and scale</p>
</blockquote>
</aside>
<p>This is a fair point! I’m the fisr to aggree that <code>CodeGen</code>, despite being a simple library might be quite the concept shift from what is usually done in Elixir. Currently, everything that generates code in Elixir is either a mix generator or a macro. <code>CodeGen</code> aims to combine the best of both approaches, but it hasn’t been validade yet. I’m mainly putting it out there as an experiment to see what peoples’ opinions are.</p>
<aside class="quote no-group" data-username="D4no0" data-post="11" data-topic="54521">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/48/33624_2.png" class="avatar"> D4no0:</div>
<blockquote>
<p>Having said all of this, this might be a good tool oriented not to end developer, but as a building block of a library that further can abstract implementation details.</p>
</blockquote>
</aside>
<p>Could you expand on this last point please? I’m not suer I understand. Do you think that end users should never have to call <code>use CodeGen, ...</code> themselves?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281470" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/codegen-simple-succint-and-customizable-code-generation-for-your-libraries/54521/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-281470" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281470"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="281476" data-post-id="281476">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tmbb" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tmbb
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="mayel" data-post="10" data-topic="54521">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mayel/48/26431_2.png" class="avatar"> mayel:</div>
<blockquote>
<p>For anyone else following along, I’ve slightly updated the previously posted code (notably to obtain the last line number of a function</p>
</blockquote>
</aside>
<p>I really like your approach, and it’s certainly a interesting option if you don’t need to generate AST dynamically. The best part is that it handles comments correctly without the need for my <code>@comment__ "..."</code> hacks.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281476" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/codegen-simple-succint-and-customizable-code-generation-for-your-libraries/54521/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-281476" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281476"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="281480" data-post-id="281480">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="D4no0" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/D4no0/120/33624_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  D4no0
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>OK I see it now, basically the main point of this library is to be able to generate and inspect the source code of the injected blocks of code, it does bring some order to code generation. My bad, I thought that the main scope of the library was to have parametric function generation by passing <code>options:</code>.</p>
<blockquote>
<p>Do you think that end users should never have to call <code>use CodeGen, ...</code> themselves?</p>
</blockquote>
<p>While it would be great a introduction of such a tool for existing code generating libraries, especially for phoenix, I don’t think code generation should be used by someone making for example an phoenix application, because as tempting as might sound, it introduces a lot of complexity.</p>
<p>In my vision, I would love to use this library something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
defmacro __using__(_env) do

@blockdoc """
  This block is responsible for injecting child specification, 
  read more about child specifications here. 
  Override this function for your custom child specification.
"""
CodeGen.block "child_spec" do

 def child_spec(init_arg) do
        default = %{
          id: __MODULE__,
          start: {__MODULE__, :start_link, [init_arg]}
        }

        Supervisor.child_spec(default, unquote(Macro.escape(opts)))
      end

      defoverridable child_spec: 1
end
end
</code></pre>
<p>In general now that I think about it, this does seem like a symptom of a larger problem, because what your library achieves is basically public code generation interfaces. Maybe AST manipulation/processing and code generation should be somehow separated, because at this moment is a big pile where you can do whatever comes to your mind.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281480" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/codegen-simple-succint-and-customizable-code-generation-for-your-libraries/54521/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-281480" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281480"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="281601" data-post-id="281601">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tmbb" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tmbb
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="D4no0" data-post="14" data-topic="54521">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/d4no0/48/33624_2.png" class="avatar"> D4no0:</div>
<blockquote>
<p>In my vision, I would love to use this library something like:</p>
</blockquote>
</aside>
<p>I don’t understand the difference bewteen this and what I already have. Is it the fact that the module defines a “normal” <code>__using__</code> macro instead of the wierd <code>__gen_code__</code> function which is called by the <code>use CodeGen</code> macro?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281601" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/codegen-simple-succint-and-customizable-code-generation-for-your-libraries/54521/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-281601" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281601"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #14"></div>
  </section>
</div>
    <div class="postbit" id="281603" data-post-id="281603">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="D4no0" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/D4no0/120/33624_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  D4no0
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>There is no difference, I just showed an example where your library would come really handy and added the <code>@blockdoc</code> that would help users understand why they would want to override that piece of injected code, I think documentation is as important as the feature itself.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281603" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/codegen-simple-succint-and-customizable-code-generation-for-your-libraries/54521/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-281603" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281603"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #15"></div>
  </section>
</div>
    <div class="postbit" id="281872" data-post-id="281872">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tmbb" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tmbb
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>The <code>@blockdoc</code> seems like a good idea for documentation purposes. But I don’t know how to implement that… Maybe it would be better to document the blocks outside the inner quoted expression? It’s hard do get the <span class="mention">@blockdoc</span> from inside the quoted expression.</p>
<p>I guess the best way to document the blocks is probably inside the <code>@moduledoc</code> of the template module, but I’m not sure. Maybe there’s a more structured way of doing it which I’m not seeing.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="281872" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/codegen-simple-succint-and-customizable-code-generation-for-your-libraries/54521/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-281872" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="281872"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #16"></div>
  </section>
</div>
    <div class="postbit" id="283830" data-post-id="283830">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tmbb" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tmbb
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>It turns out that in order to deal with compile-time dependencies properly, <code>__gen_code__(options)</code> must be a macro and not a function.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="283830" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/codegen-simple-succint-and-customizable-code-generation-for-your-libraries/54521/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-283830" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="283830"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>