Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Dialyzer analyze anonymous functions?

In my progress of learning Elixir, I am playing around with Dialyzer to put types on my functions. In this regard, I've noticed that Dialyzer doesn't seem to check the types for anonymous functions.

In the example below, I am passing an anonymous function which adds two numbers and returns a number (t::number -> number), into the all? function. Thus I am not returning boolean as promised in the all? spec (t::any -> boolean).

defmodule Exercises do                                                                         
  @spec all?([t::any], (t::any -> boolean)) :: boolean                                         
  def all?([], _), do:  true                                                                   
  def all?([h|t], con) do                                                                      
    if con.(h) do                                                                              
      all?(t,con)                                                                              
    else                                                                                       
      false                                                                                    
    end                                                                                        
  end                                                                                          

  @spec funski() :: boolean                                                                    
  def funski() do                                                                              
    all?([1,1,2], &(&1 + 1))                                                                
  end
end

Dialyzer doesn't seem to report any errors or warnings for this code, and I am curios if Dialyzer is unable to check this kind of mistakes or if I am doing something wrong.

like image 955
Michelrandahl Avatar asked Jul 09 '15 02:07

Michelrandahl


1 Answers

It seems to be a Dialyzer bug. Calling :lists.all/2 (with the arguments swapped) produces the correct warning but for some reason calling your local all?/2 function with the same spec does not.

like image 174
Eric Meadows-Jönsson Avatar answered Oct 24 '22 05:10

Eric Meadows-Jönsson