cashews

cashews

Does the Elixir compiler tree-shake any unused functions from an imported module?

In this case, considering we don’t have any overlapping function names, is it worth it to use only as a means of asserting that we’re only importing the from public function from the Ecto.Query module or will the compiler tree-shake all unused functions from Ecto.Query regardless?

import Ecto.Query, only: [from: 2]

Marked As Solved

sodapopcan

sodapopcan

The short answer is yes.

To add some pedantry, I wouldn’t call it “treeshaking” per se. import is more “opening up the module” to use for function resolution, not so much “importing” it as you would think about it in JS. All imported functions are being expanding to their fully qualified names during compilation.

While :only isn’t strictly necessary, it is nice for when someone comes blind to a function in your module and it may not be obvious where a particular function is coming from if import is used multiple times. With your example of Ecto.Query, though, I never both specifying from because it’s generally super obvious by the context of the file what from is. It’s context-dependent and, of course, a matter of taste!

But ya, unused functions are not compiled.

Also Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Just to be clear though, the functions will exist inside the Ecto.Query module regardless of whether they are used anywhere or not. import is only lexical, and calls to imported functions are translated into remote calls a la Ecto.Query.from.

sodapopcan

sodapopcan

Ya sorry, very poor wording. And reading over my response I see more poor grammar :sweat_smile: :face_with_peeking_eye:

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
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
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
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement