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: Erlang -- code

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 |>:

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

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
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
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics 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
yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

We're in Beta

About us Mission Statement