Question about coalescing operator “?:”.
#1
This works fine:
dump( notExists.key ?: 'notExistsKey' );
But this NOT works because getLang() is null:
dump( person.getLang().getId() ?: 'notExistsProp' );
Raise the error: can’t call method [getId] on object, object is null
But, accordingly to docs about colaescing operator, the element on the left is evaluated if it is null.
I have to add safe navigation (“?”) to avoid the error:
dump( person?.getLang()?.getId() ?: 'notExistsProp' );
As you can see, structs and objects work differently.
Shouldn’t it work the same way?
Many thanks.
1 post - 1 participant