Hi everyone. I found a weird compiler behavior. He...
# javascript
n
Hi everyone. I found a weird compiler behavior. Here is the row:
attrs.caption = state.toBeDeleted?.asDynamic()["name"].toString()
I'm expecting that if
state.toBeDeleted
is
null
, the rest of row will not be invoked, and
null
will be the value of the whole expression. But this row is compiled into:
Copy code
$receiver.attrs.caption = ((tmp$ = this$Content.state.toBeDeleted) != null ? tmp$ : null)['name'].toString();
And the result is
Cannot read property 'name' of null
. Am I misunderstanding something?
Are the extension functions being invoked even on
null
values?
a
Try this:
Copy code
attrs.caption = state.toBeDeleted.asDynamic()?.`name`?.toString()
asDynamic
is an extension function to
Any?
, so it will work with
null
receiver
👍 1
dynamic
is already nullable, so
null?.asDynamic()
should be the same as
null.asDynamic()
. Both expressions have type
dynamic
. Which is why the compiler didn't complain about the
["name"]
being applied to a nullable receiver
n
Thank you!