brewinstallbuzzwords

brewinstallbuzzwords

Confusion about loading static assets with :code atom and :code.priv_dir/1

Hi everyone,

I’m working on a small Elixir project that needs to include a text file as a resource. I found on some other posts that this file should exist in the project’s /priv directory and that I should get the path to priv like this: :code.priv_dir(:my_project), and then add the file name to the end using Path.join/2.

This is working fine, but what’s confusing to me is that this seems to be leveraging Erlang functions.

When I run h :code in iex I can see that it represents the Erlang code server, which led me to find this page in the Erlang docs: code — OTP 29.0.2 (kernel 11.0.2)

There, I was able to find the documentation for priv_dir/1 that I had been struggling to find on hexdocs.

So here’s my question:

Is this the generally accepted way to load a static asset in Elixir? Are there any functions in Elixir that do the same thing, but don’t require making calls to :code? Are there any other functionalities in Elixir that require making calls to Erlang functionality?

Marked As Solved

mpope

mpope

I believe you can use Application.app_dir/2 and point the second argument to the "/priv/..." file you want to access. I don’t think there is anything wrong with using :code.priv_dir/1 directly though.

Also Liked

al2o3cr

al2o3cr

There’s not a strong boundary between “Elixir stuff” and “Erlang stuff” - they both ultimately run on the BEAM, and Elixir uses a lot of Erlang functionality with only a thin wrapper on top; for instance, many of the functions in the Elixir Map module mostly exist to make the underlying Erlang functions play nice with |>:

https://github.com/elixir-lang/elixir/blob/33f9d04851a2664aeae5f5f78eeef9e5b4c38030/lib/elixir/lib/map.ex#L236-L237

Historically, it’s been much harder to use Elixir code from Erlang than the other way around, so libraries that need to function in both contexts tend to be written in Erlang. IIRC that is changing nowadays with work being done to bring Hex and rebar closer together.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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

Other popular topics Top

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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement