Rich_Morin

Rich_Morin

Enabling access to occasionally used (eg, tracing) functions?

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?

Most Liked

Rich_Morin

Rich_Morin

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.

LostKobrakai

LostKobrakai

You can also disable warnings on the callers side:
https://hexdocs.pm/elixir/Kernel.SpecialForms.html#import/2-warnings

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> someth...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement