https://kotlinlang.org logo
Title
s

scottiedog45

06/12/2019, 11:06 AM
This line is not compiling and I have no clue why :
var phoneOrTablet : String = Resources.getSystem().getBoolean(R.bool.isTablet) ? "Tablet" : "Phone"
d

Daniel Garibaldi

06/12/2019, 12:23 PM
you can write it like this
val phoneOrTablet = if(Resources.getSystem().getBoolean(R.bool.isTablet) ) "Tablet" else "Phone"
no need to specify the type, and no need to declare a VAR
or use a function fun phoneOrTablet() = if(Resources.getSystem().getBoolean(R.bool.isTablet) ) "Tablet" else "Phone"
you dont need to hold that value in a variable if you use it only once
m

Mike

06/12/2019, 12:33 PM
Bottom-line. Kotlin doesn’t have a ternary operator because
if
is an expression.
if
is unambiguous and clear to understand whereas the ternary operator has been learned, and therefore, but isn’t. Also, reduces potential confusion with the Elvis
?:
operator.
👍 2