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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanks!  I ask because this is one of those things I thought I knew the answer to then 30 mins later I still going <img src="https://forum.elixirforum.com/images/emoji/apple/upside_down_face.png?v=15" title=":upside_down_face:" class="emoji" alt=":upside_down_face:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="345236" 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/iteratively-calling-use-on-a-list-of-modules/67250/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-345236" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="345236"
                     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="345270" data-post-id="345270">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="feld" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  feld
                    <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>This one works, but not if the list you’re passing is a module attribute like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">@mods [Foo]
use MultiUse, @mods
</code></pre>
<p>In that case it errors like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">** (Protocol.UndefinedError) protocol Enumerable not implemented for {:@, [line: 12, column: 28], [{:mods, [line: 12, column: 29], nil}]} of type Tuple
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="345270" 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/iteratively-calling-use-on-a-list-of-modules/67250/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-345270" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="345270"
                     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="345272" data-post-id="345272">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>That’s why he set the module attribute within the macro.</p>
<p>The problem is that <code>use</code>, as well as <code>import</code> and <code>alias</code>, are scoped to the block they are called in, so you have to get around that somehow and it’s a little tricky.</p>
<p>For example:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Foo do
  if true do
    import String
    capitalize("foo") # all good!
  end

  capitalize("foo") # Error: `capitalize` doesn't exists
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="345272" data-batch-url="/posts/batch_likers">
                        2
                      </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/iteratively-calling-use-on-a-list-of-modules/67250/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-345272" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="345272"
                     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="345276" data-post-id="345276">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="feld" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  feld
                    <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>But I need the module attribute in the other module. Let me just lay my cards out here, this is for Telemetry/Logging. The goal is to have a telemetry module that receives events and turns them into log messages. I don’t want to end up with a 2000 line file or be forced to have multiple modules duplicating the same code</p>
<p>So in <code>start/2</code> of <code>application.ex</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir"># register the handlers
Project.Telemetry.Logs.setup()
</code></pre>
<p>and then in the module that wires everything up:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Project.Telemetry.Logs do
  @moduledoc "Transforms telemetry events to logs"

  require Logger

  @loggers     [
      Project.Telemetry.Logs.FileStore,
      Project.Telemetry.Logs.Oban,
      Project.Telemetry.Logs.Tesla
    ]

  # I need to have a use here that can "use" each of those @loggers modules
  # which will inject their handle_event/4 functions into this file

  def setup do
    events =
      Enum.reduce(@loggers, [], fn logger, acc -&gt;
        acc ++ logger.events()
      end)

    Enum.each(events, fn event -&gt;
      :telemetry.attach({__MODULE__, event}, event, &amp;__MODULE__.handle_event/4, [])
    end)
  end

  # Catchall
  def handle_event(event, measurements, metadata, _) do
    Logger.error(fn -&gt;
      "Unhandled telemetry event #{inspect(event)} with measurements #{inspect(measurements)} and metadata #{inspect(metadata)}"
    end)

    :ok
  end
end
</code></pre>
<p>example logger module:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Project.Telemetry.Logs.Oban do
  @moduledoc false

  @events [
    [:oban, :job, :exception]
  ]

  def events(), do: @events

  defmacro __using__(_opts) do
    quote do
      def handle_event([:oban, :job, :exception], _measure, meta, _) do
        Logger.error(fn -&gt;
          "[Oban] #{meta.worker} error: #{inspect(meta.error)} args: #{inspect(meta.args)}"
        end)
      end
    end
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="345276" 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/iteratively-calling-use-on-a-list-of-modules/67250/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-345276" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="345276"
                     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="345279" data-post-id="345279">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Zach’s solution does this, though, it just does it sneakily.  You could change <code>@using_modules</code> to <code>@loggers</code> in <code>MultiUse.__using__</code> then do:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def Example do
  use MultiUse, [Foo]
end
</code></pre>
<p>and this will create a <code>@loggers</code> attribute will be available in <code>Example</code>.  You could even change <code>MutiUse</code> to set the module name, like <code>use MultiUse, {[Foo, Bar], set_attribute: :logger})</code> or something.  This is why it’s tricky.</p>
<p>My feeling is that there is a better way to do this that still accomplishes your end goal, especially since calling <code>use</code>, <code>import</code>, <code>alias</code> in loops isn’t something that comes up often at all.  I’ve never faced it myself in 5+ years of Elixir. I’d have to think on it myself or perhaps someone who has more experience with <code>:telemetry</code> can advise.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="345279" 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/iteratively-calling-use-on-a-list-of-modules/67250/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-345279" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="345279"
                     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="345281" data-post-id="345281">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="feld" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  feld
                    <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>Oh I see it now, I just needed a second cup of coffee. <img src="https://forum.elixirforum.com/images/emoji/apple/coffee.png?v=15" title=":coffee:" class="emoji" alt=":coffee:" loading="lazy" width="20" height="20"></p>
<p>So I could make the canonical list of modules the one I pass to <code>use MultiUse</code> and it injects the module attribute back into the module for me to use.</p>
<p>I might be okay with that but it’s obscure enough that I’d have to leave breadcrumbs for other people working on this codebase <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>Another thought I had this morning was to just loop through a list modules in the <code>application.ex</code> <code>setup/2</code>, perhaps read via <code>Application.get_env</code>, and make that the source of truth for which telemetry-derived logs are enabled.</p>
<p>edit: yeah, I can make it work this way by putting the <code>use</code> at the bottom of each module that registers for telemetry events so the catchall will still work. This way I’m not fighting with the compiler to do something it’s not meant to do.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="345281" 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/iteratively-calling-use-on-a-list-of-modules/67250/17">Post #16</a>
	                </div>
	            </div>
              <div id="likers-container-345281" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="345281"
                     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="345299" data-post-id="345299">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="feld" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  feld
                    <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>I’m going to leave this as the solution: “Just don’t try this. It’s not worth it.”</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="345299" data-batch-url="/posts/batch_likers">
                        2
                      </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/iteratively-calling-use-on-a-list-of-modules/67250/18">Post #17</a>
	                </div>
	            </div>
              <div id="likers-container-345299" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="345299"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="345305" data-post-id="345305">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Maybe this solution was not the right one, but there might be others. Maybe we can find one in another direction?</p>
<p>Edit: I’m on mobile, <a class="mention" href="/u/sodapopcan" rel="nofollow">@sodapopcan</a> is in meeting. Frustrating hours <img src="https://forum.elixirforum.com/images/emoji/apple/sweat_smile.png?v=15" title=":sweat_smile:" class="emoji" alt=":sweat_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="345305" data-batch-url="/posts/batch_likers">
                        1
                      </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/iteratively-calling-use-on-a-list-of-modules/67250/19">Post #18</a>
	                </div>
	            </div>
              <div id="likers-container-345305" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="345305"
                     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 #18"></div>
  </section>
</div>
    <div class="postbit" id="345306" data-post-id="345306">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><em>Personally</em>, I agree that “how to call <code>use</code> in a loop” is “just not worth it.”  There are maybe some situations in libraries where it could be?  I dunno, I just wouldn’t ever suggest doing it.  But I’m actually quite interested in whatever the solution to OP’s problem ends up being—that is if there is a clean way to not have to maintain two lists of the same modules.  I assume this would involve registering the event handlers in a different way (but I’ve given it no thought, I’m responding here in between meetings).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="345306" 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/iteratively-calling-use-on-a-list-of-modules/67250/20">Post #19</a>
	                </div>
	            </div>
              <div id="likers-container-345306" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="345306"
                     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 #19"></div>
  </section>
</div>
    <div class="postbit" id="345308" data-post-id="345308">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I assume the modules are listed only once in a file. Why not make use of callbacks to collect event handlers and register handlers? This way the ‘use’ is ehhhh un’use’d</p>
<p><a href="https://github.com/BartOtten/routex/blob/16faf547fdc3a809657a24c5c97babeedd4938e1/lib/routex/processing.ex#L65" rel="noopener nofollow ugc">Example in Routex</a>. (working on this lib at the moment, so maybe all I see is a hammer right now)</p>
<p>For your use case this could look like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Processor do
ast = for module &lt;- @modules do
  apply(module, :generate_handlers, [])
ene

Module.create(MyHandlers, ast)
end


defmodule LoggerX do
def generate_handlers() do
quote do
      def handle_event([:oban, :job, :exception], _measure, meta, _) do
        Logger.error(fn -&gt;
          "[Oban] #{meta.worker} error: #{inspect(meta.error)} args: #{inspect(meta.args)}"
        end)
      end
    end
end
</code></pre>
<p>The events to collect are missing in this example but that’s not that hard to add (but still on mobile). Does this seem a viable option for your use case?</p>
<ul>
<li>we could even traverse the returned AST to know all handled events.</li>
</ul> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="345308" 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/iteratively-calling-use-on-a-list-of-modules/67250/21">Post #20</a>
	                </div>
	            </div>
              <div id="likers-container-345308" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="345308"
                     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>