Suppose you have an object with a deeply nested optional variable:
struct Box {
var value: Int?
}
struct Package {
var box: Box
}
struct Container {
var package: Package
}
var container = Container(package: Package(box: Box(value: 2)))
And you have to mutate the value of the deeply nested optional, providing a default value if it is nil, you could extract the value to a local variable, mutate it and set the optional with the result. Like so:
var value = container.package.box.value
value = (value ?? 4) * 2
container.package.box.value = value
This unnecessarily creates a local variable and this can be avoided as follows:
Notice how the code is being duplicated on either side of the assignment.
Providing a subscript with a default value makes the code more succint and elegant to read, as follows:
container.package.box.value[default: 4] *= 2
Avoiding the issue of code duplication was the motivation behind Bool's toggle() and Dictionary's subscript(_:default:) methods and I propose Optional get the same upgrade.
I'm not a fan of this, this makes Optional look like a Dictionary while it's something different entirely. The example you provided does make the code shorter but IMO it's not necessarily better in this case.