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

andrzej

10/29/2019, 11:10 AM
If I have:
Copy code
class Auto(val isNew: Boolean)
val car: Car? = Car(true)
why the following code works:
Copy code
if (car?.isNew == true) println("new car")
and the following doesn't compile:
Copy code
if (car?.isNew) println("new car")
d

Dominaezzz

10/29/2019, 11:13 AM
car?.isNew
returns a
Boolean?
instead of
Boolean
which
if
requires.
==
returns
Boolean
.
☝️ 2
a

Alessandro Mautone

10/29/2019, 11:16 AM
and if you want to avoid writing
car?.isNew == true
in favour of something more elegant you could do:
if (car?.isNew ?: false)
which will return
false
in case
car
is
null
👍 1
d

diesieben07

10/29/2019, 11:17 AM
I would argue that
car?.isNew == true
states the intent more clearly: The code runs if
isNew
is true.
☝️ 4
👍 1
a

andrzej

10/29/2019, 11:37 AM
OK, thanks, I understand now. But it looks weird
if (car?.isNew == true)
, like you know on your first course on programming you learn that you should write
if(car.isNew)
instead of
if(car.isNew == true)
k

karelpeeters

10/29/2019, 11:40 AM
Yeah, I disagree with @diesieben07 and the InteliJ inspection: I think
?: false
is better because it emphasizes what happens if it's
null
. In the end it's just an opinion 🤷‍♂️
☝️ 3
d

diesieben07

10/29/2019, 11:41 AM
Your first course in programming wasn't in a null safe language like Kotlin though ;)
😄 6
But yes, Karel is right, in the end it's a matter of opinion
🙏 1
b

Burkhard

10/29/2019, 12:07 PM
It is, but the kotlin coding conventions state that you should use
nullable == true
instead of
nullable ?; false
. So even thoug I prefer the latter I use the first. https://kotlinlang.org/docs/reference/coding-conventions.html#using-nullable-boolean-values-in-conditions
👍 5
t

thanksforallthefish

10/29/2019, 12:14 PM
maybe then this is a bug in intellij inspector, if it suggests things contrary to convention it is not good. if I have to check what is the convention for every piece of code I write I would be a sloth, if intellij warns me I will learn by assimilation at some point
k

karelpeeters

10/29/2019, 12:26 PM
No Intelij does suggest
?: false
->
== true
as per the official convention.
t

thanksforallthefish

10/29/2019, 1:00 PM
I misread you, there was an
and
I missed
a

andrzej

10/29/2019, 4:18 PM
I think the main source of confusion in the case of
nullable == true
(why it works) is the fact that Kotlin does a little magic here:
Copy code
a == b is translated to:
a?.equals(b) ?: (b === null)
source: https://kotlinlang.org/docs/reference/equality.html#structural-equality 🤯
k

karelpeeters

10/29/2019, 7:40 PM
Well arguably that's not really magic, that just makes
==
behave as expected instead of throwing nullpointers like Java's
equals
.
a

andrzej

10/30/2019, 8:51 AM
that
just makes == behave as expected
I call magic. It shouldn't work when you look at it, knowing how operators in Kotlin work (how could you call
equals
method on null), but it works.
2 Views