fhunleth

fhunleth

Co-author of Nerves

Handling Elixir 1.17 compiler warnings on unused optional dependencies

I’m trying to reduce the number of Elixir 1.17 warnings in my projects, and I have questions with optional dependencies.

One example that triggers a new warning is Jason’s optional dependency on Decimal. If you create an empty project, add :jason to the dependencies and compile, you’ll see this:

==> jason
    warning: Decimal.new/1 is undefined (module Decimal is not available or is yet to be defined)
    │
 94 │         decimal.new(string)
    │                 ~
    │
    └─ (jason 1.4.3) lib/decoder.ex:94:17: Jason.Decoder.float_decode_function/1

warning: struct Decimal.Error is undefined (module Decimal.Error is not available or is yet to be defined)
└─ (jason 1.4.3) lib/decoder.ex: Jason.Decoder.float_decode_function/1

     warning: Decimal.to_string/2 is undefined (module Decimal is not available or is yet to be defined)
     │
 242 │     [?", decimal.to_string(value, :normal), ?"]
     │                  ~
     │
     └─ (jason 1.4.3) lib/encode.ex:242:18: Jason.Encode.struct/4

     warning: Decimal.to_string/1 is undefined (module Decimal is not available or is yet to be defined)
     │
 231 │     [?", decimal.to_string(value), ?"]
     │                  ~
     │
     └─ (jason 1.4.3) lib/encoder.ex:231:18: Jason.Encoder.Decimal.encode/2

There’s code that silenced a similar warning previously, but Elixir 1.17 is smarter. This is really cool that it can be detected, but I was wondering if there was preferred way of resolving warnings like this.

For example, placing the following compile-time checks around the Decimal-using functions would silence the Jason warnings:

if Code.ensure_loaded(Decimal) == {:module, Decimal} do
  def fun_that_uses_decimal() do
    ...
  end
end

Since this comes up with other libraries too, I’d like to be consistent and I’m not sure if there are subtle gotchas that this introduces.

Most Liked

LostKobrakai

LostKobrakai

Code.ensure_loaded?/2 has been what I’ve seen quite consistently being used around optional dependencies over the years, e.g.:

fhunleth

fhunleth

Co-author of Nerves

Perfect. Thank you.

Where Next?

Popular in Questions Top

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
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
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
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
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
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement