# Optional Return

**URL:** <https://forums.swift.org/t/optional-return/17089>\
**Category:** Pitches\
**Created:** [October 16, 2018, 3:06pm UTC](https://forums.swift.org/t/optional-return/17089 "2018-10-16T15:06:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [October 16, 2018, 3:06pm UTC](https://forums.swift.org/t/optional-return/17089/1 "2018-10-16T15:06:14Z")

</div>

I don't know if this idea has surfaced before, but I've found myself doing this way too often:

```swift
func foo(_ value: Int?) -> Int {
    if let value = value {
        return value
    }
    // Do something else...
}

```

I wonder, if this syntax sugar is something that Swift would benefit from:

```swift
func foo(_ value: Int?) -> Int {
    return? value
    // Do something else...
}

```

The `return?` statement takes an expression of type `R?` where `R` is the return type of the enclosing function and returns if and only if the expression is not `nil`.

This would be particularly useful in combination with optional chaining:

```swift
func foo(_ value: Int?) -> String {
    return? value.map { "\($0)" }
    // Do something else...
} 

```

What do you guys think?

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [October 16, 2018, 4:18pm UTC](https://forums.swift.org/t/optional-return/17089/2 "2018-10-16T16:18:48Z")

</div>

Implicit control flow around return seems like a great source of bugs. Remember gotofail.

---

<div class="post-metadata">

**Author:** ![Tino](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tino/32/1073_2.png) [@Tino](https://forums.swift.org/u/Tino)\
**Post date:** [October 16, 2018, 5:10pm UTC](https://forums.swift.org/t/optional-return/17089/3 "2018-10-16T17:10:24Z")

</div>

I‘ve never used nor seen this pattern before. I don‘t consider that as a valid motivation for opposition, but afaics, you can easily avoid the boilerplate:

```
let fooResult = optionalValue ?? foo()

```

What‘s wrong with doing it this way?

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [October 16, 2018, 6:25pm UTC](https://forums.swift.org/t/optional-return/17089/4 "2018-10-16T18:25:07Z")

</div>

> [@technogen](#):
>
> I don't know if this idea has surfaced before [...]
> 
> ```swift
> return? value
> 
> ```

Yet it does: [SE-0231 — Optional iteration - #38 by gwendal.roue](https://forums.swift.org/t/se-0231-optional-iteration/16737/38) 😉
