Coalescing operator

Question about coalescing operator “?:”.

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 Like


I believe anything in “” is evaluated as a string

Structs handle the coalescing operator because they check for the existence of keys.

Objects need to handle nulls as Lucee supports NULL operators