nathanl
From the docs on import:
Note that
importis 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/2is only visible within that specific function.duplicate/2won’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?
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project.
My initial shotgu...
New
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Just a general thread to post chat/news/info relating to AI/ML stuff that may be relevant for Nx now or in the future. Got anything to sh...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Chat & Discussions>Discussions
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 4 of 4 Posts
lud
Sometimes in a test helper in a test file I import
Ecto.Querybecause that function is the only place where we will interact with the DB.dimitarvp
I absolutely would and have used
import Ecto.Queryinside 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 theimportis 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:onlyreligiously or, like you are demonstrating here – just limit the scope of theimport.It’s a very valid technique IMO. Though nowadays I lean to using
:onlyor reducing the sizes of modules / functions more than reaching for lexically scopedimport.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.
sodapopcan
Consider as well libraries that (judiciously) override kernel functions, for example: Image’s
Mathmodule.I’m all for importing inside functions in scenarios like you describe and even importing just above the function where it is first used. Any push back is very much a “this is the way we’ve always done it” scenario (angry monkeys!). It makes no sense to me that you would want to spread these things apart. Same goes for mandating that module attributes must be at the top and that private functions must be at the bottom.
I never considered this and could have come in handy in some code I no longer work in