No Ternary Operator :(


While programming with kotlin I wanted to use the ternary operator something like the following

condition ? true_result : false_result.

I couldn’t find it anywhere in the documentation, I went to the github repo and took a look to see if I could find the grammar for kotlin. Sure enough they have an expressions file. I took a look and unfortunately wasn’t able to find any indication of a ternary operator. I figured this might caused by the existing use of the null check operator ?

Unfortunately kotlin doesn’t have this in the language, the closest thing that is available is this:

val max = if (a > b) a else b

Not horrible but not great either.

Leave a Reply

Your email address will not be published. Required fields are marked *