Optional Chaining in Latte: $var?->call()?->elem[1]?->item
- David Grudl
- Nette Core | 8218
I tried to add optional chaining to Latte (2.6-dev):
{if $blogPost?->count('*')} // means if (isset($blogPost) && $blogPost->count('*'))
...
{/if}
{$order->item?->name} // means isset($order->item) ? $order->item->name : null
So please try it, if it does not interfere with something, so I can release it soon.
- David Grudl
- Nette Core | 8218
Unfortunately, there is one complication. Latte knows the so-called „short
ternary operator“ {$a ? $b}
, where the third part is missing. It
means $a ? $b : null
.
So it is then unclear whether this expression $a->items?['1']
means short ternary $a->items ? ['1'] : null
or optional
chaining isset($a->items['1']) ? $a->items['1'] : null
.
From my point of view, it would be better to prefer optional chaining, but it is BC break.
- David Grudl
- Nette Core | 8218
It's not entirely accurate that this $order->item?->name
is translated as
isset($order->item) ? $order->item->name : null
. In fact,
a null coalescing operator is used to remove twice evaluations and intermediate
values are cached.
So $order->item?->name
Latte translates to
(($_tmp = $order->item ?? null) === null ? null : $_tmp->name)
.