# SE-0477: Default Value in String Interpolations

**URL:** <https://forums.swift.org/t/se-0477-default-value-in-string-interpolations/79302>\
**Category:** Proposal Reviews\
**Created:** [April 16, 2025, 1:51pm UTC](https://forums.swift.org/t/se-0477-default-value-in-string-interpolations/79302 "2025-04-16T13:51:00Z")\
**Posts on this page:** 1\
**Showing post:** 28

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [April 24, 2025, 7:52pm UTC](https://forums.swift.org/t/se-0477-default-value-in-string-interpolations/79302/28 "2025-04-24T19:52:47Z")

</div>

I'm all in favor of this as a tactical solution to the optional interpolation problem. But I wanted to float a more general solution, for completeness:

The addition of an `Either` type into the standard library is a generally useful idea. The big win for such a nominal type is that you can give it protocol conformances for when both the underlying types conform, e.g.

```swift
enum Either<Left, Right> {
    case left(Left)
    case right(Right)
}

extension Either: CustomStringConvertible
where Left: CustomStringConvertible,
      Right: CustomStringConvertible
{
  var description: String {
    switch self {
    case let .left(l): return l.description
    case let .right(r): return r.description
    }
  }
}

```

This is especially useful for things like when you want to return one of two different collection types. An `Either` conforming to `Collection` is _vastly_ more performant that `any Collection` or `AnyCollection`.

Why this is relevant here is you can define a cousin to the `??` operator, `?|`, the "nil alternative operator:

```swift
infix operator ?|: NilCoalescingPrecedence

func ?|<L, R>(_ maybe: L?, _ or: @autoclosure ()->R) -> Either<L, R> {
    switch maybe {
    case let l?: .left(l)
    case nil: .right(or())
    }
}

```

Unlike `??`, the right-hand side is not of the wrapped type, but of an arbitrary type.

This can then be used to solve things like string-interpolating an optional with a default value:

```swift
    let maybeInt: Int? = Int("x")
    print("\(maybeInt ?| "<nil>")") // prints "<nil>"

```

Is this better than the more targeted solution here? Probably not, but I think this kind of operator, and `Either` more broadly, has a lot of general utility and this is one example of that.

---

_[View the full topic](https://forums.swift.org/t/se-0477-default-value-in-string-interpolations/79302)._
