Bicep safe-dereference operator
The safe-dereference operator provides a way to access properties of an object or elements of an array in a safe manner. It helps to prevent errors that can occur when attempting to access properties or elements without proper knowledge of their existence or value.
safe-dereference
<base>.?<property>
<base>[?<index>]
A safe-dereference operator applies a member access, .?<property>
, or element access, [?<index>]
, operation to its operand only if that operand evaluates to non-null; otherwise, it returns null. That is,
- If
a
evaluates tonull
, the result ofa.?x
ora[?x]
isnull
. - If
a
is an object that doesn't have anx
property, thena.?x
isnull
. - If
a
is an array whose length is less than or equal tox
, thena[?x]
isnull
. - If
a
is non-null and has a property namedx
, the result ofa.?x
is the same as the result ofa.x
. - If
a
is non-null and has an element at indexx
, the result ofa[?x]
is the same as the result ofa[x]
The safe-dereference operators are short-circuiting. That is, if one operation in a chain of conditional member or element access operations returns null
, the rest of the chain doesn't execute. In the following example, .?name
isn't evaluated if storageAccountsettings[?i]
evaluates to null
:
param storageAccountSettings array = []
param storageCount int
param location string = resourceGroup().location
resource storage 'Microsoft.Storage/storageAccounts@2022-09-01' = [for i in range(0, storageCount): {
name: storageAccountSettings[?i].?name ?? 'defaultname'
location: storageAccountSettings[?i].?location ?? location
kind: storageAccountSettings[?i].?kind ?? 'StorageV2'
sku: {
name: storageAccountSettings[?i].?sku ?? 'Standard_GRS'
}
}]
Next steps
- To run the examples, use Azure CLI or Azure PowerShell to deploy a Bicep file.
- To create a Bicep file, see Quickstart: Create Bicep files with Visual Studio Code.
- For information about how to resolve Bicep type errors, see Any function for Bicep.
Feedback
https://aka.ms/ContentUserFeedback.
În curând: Pe parcursul anului 2024, vom elimina treptat Probleme legate de GitHub ca mecanism de feedback pentru conținut și îl vom înlocui cu un nou sistem de feedback. Pentru mai multe informații, consultați:Trimiteți și vizualizați feedback pentru