Crowdhailer
Help with dialyzer warning on typespec that includes anonymous function
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
First Post!
NobbZ
You have the function type wrong. I’ll try to take a closer look into it in a couple of minutes.
Most Liked
sasajuric
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
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.
Last Post!
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









