nathanl

nathanl

Do you ever `import` inside a function?

From the docs on import:

Note that import is lexically scoped too. This means that we can import specific macros or functions inside function definitions:

defmodule Math do
  def some_function do
    import List, only: [duplicate: 2]
    duplicate(:ok, 10)
  end
end

In the example above, the imported List.duplicate/2 is only visible within that specific function. duplicate/2 won’t be available in any other function in that module (or any other module for that matter).

The example given is silly; it’s shorter and clearer to call List.duplicate(:ok, 10). But you have to import Ecto.Query to use its macros. I’ve used a function-local import when only one function in a module needs import Ecto.Query, but it’s been flagged in code review.

When, if ever, would you use import this way?

Most Liked

dimitarvp

dimitarvp

I absolutely would and have used import Ecto.Query inside a single function, a good amount of times. Trouble is that many don’t like it for reasons they can’t explain very well themselves and deny a PR approval until the import is extracted top-side.

I partially get their point: if you get to a point where tracking the source of an imported function becomes difficult then that obviously means you should break your module apart on several smaller ones. I am a huge fan of such incremental refactorings.

But IMO some of the import-s (when not using :only) can bring in quite a lot of context that might be surprising or lead to weird compile-time warnings and runtime errors if f.ex. you import two modules and they have overlapping function names. For those cases either use :only religiously or, like you are demonstrating here – just limit the scope of the import.

It’s a very valid technique IMO. Though nowadays I lean to using :only or reducing the sizes of modules / functions more than reaching for lexically scoped import.

lud

lud

Sometimes in a test helper in a test file I import Ecto.Query because that function is the only place where we will interact with the DB.

LostKobrakai

LostKobrakai

There’s also more more parts to lexical scopes than just function definitions. E.g. you could do an import outside of a module definition and it would affect the whole file, you could have an import within an anonymous function and it would only apply to that function, …

Personally like people before I’d also argue having a single function do an import can be perfectly fine.

Where Next?

Popular in Discussions Top

jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
Donovan
Hello everyone, I’m so glad to have discovered this awesome community. Thanks for creating it! This is my second post, and apologies for...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
Fl4m3Ph03n1x
Background This question comes mainly from my ignorance. Today is Black Friday, one of my favorite days of the year to buy books. One boo...
New
IVR
Hi all, I’ve seen a number of related threads in the past, but I’d still be very curious to hear an up-to-date opinion on this topic. I...
New
Qqwy
I would like to spark a discussion about the static access operator: .. For whom does not know: it is used in Elixir to access fields of...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
Owens
Hello all, I am developing a new mobile app with Flutter frontend and Phoenix backend. The mobile app has real-time task management and c...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

We're in Beta

About us Mission Statement