Quantcast
Channel: Lucee Dev - Latest topics
Viewing all articles
Browse latest Browse all 435

Coalescing operator

$
0
0

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

Read full topic


Viewing all articles
Browse latest Browse all 435

Trending Articles