https://kotlinlang.org logo
#announcements
Title
# announcements
b

bernhard

02/14/2017, 5:52 PM
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

mg6maciej

02/14/2017, 6:01 PM
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

bernhard

02/14/2017, 6:03 PM
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

mg6maciej

02/14/2017, 6:06 PM
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

bernhard

02/14/2017, 6:21 PM
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