however, syntax for assigning val inside of an if ...
# announcements
b
however, syntax for assigning val inside of an if condition would be quite helpful in cleaning up code, specially when used in some
if-else if-else
constructs
m
bernhard: I'm not a fan of adding language specific constructs just to save half of line of code and making it harder to understand when reading.
b
still I really like the if-let syntax of swift and would be glad to see it in kotlin too maybe it shouldn't be generally available to declare vals in condition blocks. I don't really like this either (altough there are few cases where it makes sense).
but a simple swift style if-let for checking nullable objects and smart casting them with the option for an else block would be quite nice, I have several parts in my code where this would be handy
m
Smart casts cover it a bit more broadly than
!= null
check
if let
provides.
Chaining if elses would be an example of where it could be useful, but I don't remember chaining on different variables in every condition.
For single if-else I think having
val
assignment one line above is even clearer than
if let
.
I like to follow Zen of Python (https://www.python.org/dev/peps/pep-0020/) and "explicit is better than implicit" rule applies here, where you explicitely show you are checking against null. 🙂
b
yeah it's definitely clearer when reading, but it would be a shorthand check to what the stdlib
?.let
already does just in combination with an if statement, where you could check multiple values simultaneously instead of having them assigned to local vals each and then again checking each
!= null