Fast64 & NIF & warning: incompatible types given to File.write!/2

We’re migrating one of our projects to Elixir 1.18.4 (from 1.16.2). Facing a lot of issues because of --warnings-as-errors, but most of them are easily fixable. And, it actually found a couple of bugs in our code base, like it!

However, there’s one warning I don’t know how to fix yet. It’s a simple line:

File.write!(printed_pdf, Fast64.decode64(pdf_data))

That yields the following warning:

Compiling 28 files (.ex)
    warning: incompatible types given to File.write!/2:

        File.write!(printed_pdf, Fast64.decode64(pdf_data))

    given types:

        dynamic(), none()

    the 2nd argument is empty (often represented as none()), most likely because it is the result of an expression that always fails, such as a `raise` or a previous invalid call. This causes any function called with this value to fail

That’s because it’s NIF and the default encode64 implementation is raising.

How do you all cope with situations like this?

1 Like

:waving_hand:

I wonder if using erlang — erts v16.0.1 solves it?

def encode64(_data) do
  :erlang.nif_error(:not_loaded)
end

That’s what I use in my NIFs and I don’t seem to get incompatible types warnings.

2 Likes

Lovely, thank you very much! It fixes the warnings.

Here’s the fast64_elixir PR.