This is something I've wanted for a long time.  If we have a `compare` 
function on all the main type modules like String, Atom, Binary, Integer, 
Float, etc... then we'd be able to pass in the module atom as a witness 
through functions along with the value type, it is one of the best ways you 
can do type based programming in a purely dynamic language.
.


On Thursday, October 12, 2017 at 7:05:57 PM UTC-6, miwee wrote:
>
> consider following code segment
>
>   def valid_row?(%{"acu" => acu}) when is_number(acu) do
>     acu_limit = Application.get_env(:beam_checker, :acu_limit)
>     acu > acu_limit
>   end
>
> since elixir/erlang comparison operator is very broad, the above code will 
> never throw any exception irrespective of what is the data type or value 
> for acu and acu_limit. But the end result is totally unexpected in cases 
> where wrong types are supplied. two actual error cases which happened (and 
> took some time to discover due to no exception) :
> 1) acu_limit was not defined in config, hence acu_limit got value nil
> 2) acu_limit was defined as string value in config
>
> possible options for discussion:
> 1) can we have Integer.compare and Float.compare functions. That way 
> whenever numeric comparisons are needed, these dedicated functions can be 
> used. (preferred)
> 2) strict comparison version of > and < operators (something along the 
> lines of == and ===, but for comparison)
>
> option not for discussion
> 1) make a private comparison function, which throws an error if input 
> values are not numbers. this is what I currently did.
>
> thanks 
> miwee  
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/elixir-lang-core/b4e0bfd2-ae1b-4cfa-8fcf-f1546a7edd64%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to