Right now when you write something like

x in -1..1

elixir compiles it to

is_integer(x) and x >= -1 and x <= 1


If we were to write

x in Enum.to_list(-1..1)

It would have the exact same effect (as x would have to be an int that is 
bigger than or equal to -1 and lesser than or equal to 1 to pass) which 
makes sense when we look at Ranges as being just a lazily-evaluated 
enumerable (like a Stream).

But i think that we should consider a range (atleast in the context of an 
"in" operation) equal to a space. Right now if we were to check if a float 
is inside a range, it would always fail (which, again, makes sense if we 
see the Range as a lazy enum).

What i propose is that elixir accepts floats when executing "in" for a 
range. I believe it semantically makes sense (as in "0.5 is a number in the 
-1..1 range ?" instead of "0.5 is inside the list of integer produced by 
the -1..1 range/sequence")

So, tl;dr, changing Kernel's macro in_range_literal/3 to

(is_integer(x) or is_float(x)) and increasing_compare(a, b, c)


That way we can easily check things like

def valid_probability?(val), do: val in 0..1

def contains_enough_products(amount), do: is_integer(amount) and amount in 
1..10


Note that in the above example i shown that you explicitly checks if the 
value is integer when you want it to be, which i believe is not a bad 
side-effect to this proposal (and i think that it is not obvious that this 
is the default behaviour, specially because the documentation doesn't 
states that ranges won't succeed with floats)

-- 
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/4a5be52c-59eb-43e5-bc40-abcf5034a59f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to