Crowdhailer

Crowdhailer

Creator of Raxx

I have the following small bind (or flat_map) implementation for error tuples.

@spec bind({:ok, a} | {:error, reason}, function(a) :: {:ok, b} | {:error, reason}) ::
        {:ok, b} | {:error, reason}
      when a: any, b: any, reason: term
def bind({:ok, value}, func) when is_function(func, 1), do: func.(value)
def bind({:error, reason}, _func), do: {:error, reason}

When I use this function as follows

@spec safe_div(integer, integer) :: {:ok, float} | {:error, :zero_division}
def safe_div(_, 0) do
  {:error, :zero_division}
end

def safe_div(a, b) do
  {:ok, a / b}
end

OK.bind({:ok, 4}, &safe_div(3, &1))

I see the following warning.

test/integration.ex:5:no_return
Function run/0 has no local return.
________________________________________________________________________________
Please file a bug in https://github.com/jeremyjh/dialyxir/issues with this message.

Failed to parse warning:
[{:"(", 1}, {:"{", 1}, {:atom_full, 1, '\'ok\''}, {:",", 1}, {:atom_part, 1, 'a'}, {:"}", 1}, {:|, 1}, {:"{", 1}, {:atom_full, 1, '\'error\''}, {:",", 1}, {:atom_part, 1, 'r'}, {:atom_part, 1, 'e'}, {:atom_part, 1, 'a'}, {:atom_part, 1, 's'}, {:atom_part, 1, 'o'}, {:atom_part, 1, 'n'}, {:"}", 1}, {:",", 1}, {:atom_part, 1, 'f'}, {:atom_part, 1, 'u'}, {:atom_part, 1, 'n'}, {:atom_part, 1, 'c'}, {:atom_part, 1, 't'}, {:atom_part, 1, 'i'}, {:atom_part, 1, 'o'}, {:atom_part, 1, 'n'}, {:"(", 1}, {:atom_part, 1, 'a'}, {:")", 1}, {:::, 1}, {:"{", 1}, {:atom_full, 1, '\'ok\''}, {:",", 1}, {:atom_part, 1, 'b'}, {:"}", 1}, {:|, 1}, {:"{", 1}, {:atom_full, 1, '\'error\''}, {:",", 1}, {:atom_part, 1, 'r'}, {:atom_part, 1, 'e'}, {:atom_part, 1, 'a'}, {:atom_part, 1, 's'}, {:atom_part, 1, 'o'}, {:atom_part, 1, 'n'}, {:"}", 1}, {:")", 1}, {:->, 1}, {:"{", ...}, {...}, ...]


Legacy warning:
test/integration.ex:12: The call 'Elixir.OK':bind({'ok', 4},fun((_) -> {'error','zero_division'} | {'ok',float()})) breaks the contract ({'ok',a} | {'error',reason},function(a)::{'ok',b} | {'error',reason}) -> {'ok',b} | {'error',reason} when a :: any(), b :: any(), reason :: term()
________________________________________________________________________________
test/integration.ex:42:unused_fun
Function fetch_key/2 will never be called.
________________________________________________________________________________
done (warnings were emitted)

I can’t make head or tail of it. Any help would be appreciated.
You can see the full code on this PR. Add remaining type specs by CrowdHailer · Pull Request #54 · CrowdHailer/OK · GitHub

Showing Posts 1 to 7

NobbZ

NobbZ

You have the function type wrong. I’ll try to take a closer look into it in a couple of minutes.

sasajuric

sasajuric

Author of Elixir In Action

To specify a lambda, you should use (arg1, arg2, ... -> result). With that change, the following typespec should be working:

@spec bind({:ok, a} | {:error, reason}, (a -> {:ok, b} | {:error, reason})) ::
          {:ok, b} | {:error, reason}
        when a: any, b: any, reason: term
NobbZ

NobbZ

Yes exactly, that was my intuition as well, but at the time of my writing, I was only on a mobile, the following diff works for me:

diff --git a/lib/ok.ex b/lib/ok.ex
index dd333dc..b640eb0 100644
--- a/lib/ok.ex
+++ b/lib/ok.ex
@@ -44,3 +44,3 @@ defmodule OK do
   """
-  @spec bind({:ok, a} | {:error, reason}, function(a) :: {:ok, b} | {:error, reason}) ::
+  @spec bind({:ok, a} | {:error, reason}, (a -> {:ok, b} | {:error, reason})) ::
           {:ok, b} | {:error, reason}
Crowdhailer

Crowdhailer OP

Creator of Raxx

Interesting. I wonder why this one was not a problem.

https://github.com/CrowdHailer/OK/blob/master/lib/ok.ex#L27-L30

NobbZ

NobbZ

Because you create a parameter named function(a) which shall have the type b, which again is an alias for any. And there is no value that contradicts any.

Crowdhailer

Crowdhailer OP

Creator of Raxx

Thanks.

One further question.
The parameters in the spec (a, b, reason) are just alias for anyway?
There is no way to make dialyzer realize that if given a function that returns integers the return type will be {:ok, integer} is there?

i.e.

{:ok, :my_atom} = OK.bind({:ok, 4}, &safe_div(3, &1))

That should raise an error becase the return type is really {:ok, integer} | {:error, term}. As far as I am aware there is no way to teach dialyzer that

NobbZ

NobbZ

That all occurrences of a should have the same type is only a semantic and mostly of documentation also purpose. Dialyzer does not check if this is actually true.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews