``` fun foo(bar: Long) { ... } //elsewhere: fun wh...
# announcements
b
Copy code
fun foo(bar: Long) { ... }
//elsewhere:
fun whatever(baz: Long?) = return when {
  baz ?: 0 > 0 -> foo(baz) // here
  else -> null
}
why is there no smart cast for baz being non null here?
k
The compiler simply doesn't try to be too smart about stuff like this.
b
OK, thanks for the reply
I don't like the
!!
operator but I guess it is safe here.
k
Can't you just do
baz != null && baz > 0
or something?
h
Or maybe change whatever to whatever(baz: Long) and use baz ?: 0 at the call site.
b
the question was more about why the compiler doesn't smart cast rather than how to deal with the consequences
k
But there’s nothing to smart-cast. If
baz
was
null
, it still continues to be
null
k
Yes, but if
baz ?: 0 > 0
then
baz
is certainly not null.
👍 1
k
Aha. Right. Missed that