would be nice if range checks caused smart casting...
# language-proposals
d
would be nice if range checks caused smart casting:
Copy code
if (pin?.length != 36) throw Exception()
// pin is smart cast here
Copy code
if (pin?.length !in (8..36)) throw Exception()
// pin is not smart cast here
d
Are you sure the latter is legal? It should be calling
operator fun contains(value: Int)
Which it can't do with a nullable type
d
there is a version of contains with nullable parameter in the stdlib
d
Moreover, I think just doing
x == null || x ...
is way more readable
d
If contracts were allowed for operator functions (not sure why not...?), this could be solved easily by adding the following:
Copy code
contract {
  returns(true) implies(element != null)
}
d
Perhaps because operator functions are quite implicit
So having some smart cast that depends on which exact function was resolved might be fragile