Rich_Morin

Rich_Morin

This question is a bit convoluted, so please bear with me…

I have some common functions that I use occasionally for tracing and such. For example, Common.ii/2 provides a shorthand version of IO.inspect/2:

foo = bar
|> ii(:bar)
...

If I want to keep a trace around for later, I comment it out. To keep my code tidy, I use an import statement:

import Common, only: [ ii: 2 ]

However, if none of the traces are currently in use, this generates a warning:

warning: unused import Common

So, I have to comment out the import line:

# import Common, only: [ ii: 2 ]

My problem arises when I import more than one function, only one of which may need to be commented out:

import Common, only: [ ii: 2, str_list: 1 ]

I tried using multiple import statements, but this fails silently (only the last one takes effect):

import Common, only: [ ii: 2 ]
import Common, only: [ str_list: 1 ]

So, I’m forced to use some rather awkward code formatting:

import Common, only: [ # ii: 2,
  str_list: 1 ]

Comments? Clues? Suggestions?

Showing Posts 1 to 8

NobbZ

NobbZ

use the module and return a quoted expression that is generated: true.

Rich_Morin

Rich_Morin OP

Thanks for the prompt response. However, after Googling a bit and trying things out, I don’t think this will work for my use case.

Problem is, my Common module (following @PragDave’s suggested approach) only contains defdelegate statements. So, when I wrap my tracing functions (in Common.Tracing) in the quote, Elixir gets very annoyed.

More generally, this approach seems a bit arcane, given the (seemingly) simple thing I’m trying to accomplish. Is there some fundamental reason why multiple import statements can’t be used for the same module?

NobbZ

NobbZ

I do not understand what you consider arcane there?

defmodule M do
  defmacro __using__(args) do
    quote generated: true do
      import __MODULE__, unquote(args)
    end
  end

  defdelegate …
end

PS: I do not know much about @pragdaves approach, except that I don’t use it, and also I’m not sure if I like the additional reductions it does cost.

Rich_Morin

Rich_Morin OP

I tried playing with this a bit and it caused my compile to blow up. This seems like a rathole, so I think I’ll punt for the moment…

NobbZ

NobbZ

Yes, sorry, typed that on my mobile. This works for me:

iex(1)> defmodule M do
...(1)>   defmacro __using__(args) do
...(1)>     quote generated: true do
...(1)>       import unquote(__MODULE__), unquote(args)
...(1)>     end
...(1)>   end
...(1)>
...(1)>   defdelegate ii(a), to: IO, as: :inspect
...(1)> end
iex(2)> defmodule M2 do
...(2)>   use M, only: [ii: 1]
...(2)>   def f(x), do: ii(x)
...(2)> end
iex(3)> M2.f 4
4
4
Rich_Morin

Rich_Morin OP

Thanks; that works nicely. I (carefully!) modified my code base to use it and it passes both dialyzer and my tests.

I’m not sure if I like the additional reductions it does cost.

I doubt whether the added reductions have any significant impact, unless a function is being called in the middle of a tight loop. Given that these functions are part of a library or component, this isn’t all that likely.

FWIW, I’m very happy with PragDave’s approach. It lets me organize my implementation code into a hidden set of small, cohesive modules. It also lets me write public (and thus testable and documentable) functions that aren’t part of the “official” API.

LostKobrakai

LostKobrakai

You can also disable warnings on the callers side:

Rich_Morin

Rich_Morin OP

Cool! I like this better than the use approach, for multiple reasons:

  • It gets rid of a macro in my code base.
  • I can use import for all of my libraries.
  • The caller controls the module usage.
  • The whole thing seems more explicit.

Thanks.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews