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 to null, the result of a.?x or a[?x] is null.
  • If a is an object that doesn't have an x property, then a.?x is null.
  • If a is an object that doesn't have an element at index x, then a[?x] is null
  • If a is an array whose length is less than or equal to x, then a[?x] is null.
  • If a is non-null and has a property named x, the result of a.?x is the same as the result of a.x.
  • If a is non-null and has an element at index x, the result of a[?x] is the same as the result of a[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@2023-04-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