<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/__(_:_:)-9xjze",
  "metadataVersion" : "0.1.0",
  "role" : "Operator",
  "symbol" : {
    "kind" : "Operator",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s2qqoiyxxSgn_xyKXKtKRi_zlF"
  },
  "title" : "??(_:_:)"
}
-->

# ??(_:_:)

Performs a nil-coalescing operation, returning the wrapped value of an
`Optional` instance or a default value.

```
func ?? <T>(optional: consuming T?, defaultValue: @autoclosure () throws -> T) rethrows -> T where T : ~Copyable
```

## Parameters

`optional`

An optional value.

`defaultValue`

A value to use as a default. `defaultValue` is the same
type as the `Wrapped` type of `optional`.

## Discussion

A nil-coalescing operation unwraps the left-hand side if it has a value, or
it returns the right-hand side as a default. The result of this operation
will have the non-optional type of the left-hand side’s `Wrapped` type.

This operator uses short-circuit evaluation: `optional` is checked first,
and `defaultValue` is evaluated only if `optional` is `nil`. For example:

```
func getDefault() -> Int {
    print("Calculating default...")
    return 42
}

let goodNumber = Int("100") ?? getDefault()
// goodNumber == 100

let notSoGoodNumber = Int("invalid-input") ?? getDefault()
// Prints "Calculating default..."
// notSoGoodNumber == 42
```

In this example, `goodNumber` is assigned a value of `100` because
`Int("100")` succeeded in returning a non-`nil` result. When
`notSoGoodNumber` is initialized, `Int("invalid-input")` fails and returns
`nil`, and so the `getDefault()` method is called to supply a default
value.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)