# SE-0380: \`if\` and \`switch\` expressions

**URL:** <https://forums.swift.org/t/se-0380-if-and-switch-expressions/61899>\
**Category:** Proposal Reviews\
**Created:** [December 7, 2022, 5:47pm UTC](https://forums.swift.org/t/se-0380-if-and-switch-expressions/61899 "2022-12-07T17:47:41Z")\
**Posts on this page:** 1\
**Showing post:** 45

<div class="post-metadata">

**Author:** ![Paul\_Cantrell](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/paul_cantrell/32/16764_2.png) [@Paul\_Cantrell](https://forums.swift.org/u/Paul_Cantrell)\
**Post date:** [December 11, 2022, 5:50pm UTC](https://forums.swift.org/t/se-0380-if-and-switch-expressions/61899/45 "2022-12-11T17:50:51Z")

</div>

> [@nnnnnnnn](#):
>
> We should be able to look at them as replaceable by a function call, just like any other expression you can place on the right-hand side of an assignment.

It's not clear that we need to impose that limitation. There’s no precedent for that rule in other imperative languages that support conditional expressions. Swift's neighbors generally either (1) support function returns from condition expressions, (2) only have the ternary operator (or something isomorphic to the ternary), or (3) are functional languages that do not have `return` statements at all.

Rust allows it:

```swift
fn foo(array: &[i32]) -> i32 {
  let value = if array.is_empty() {
    0
  } else {
    return 3 // ✅ returns from function
  };
  // do stuff with value
}

```

Ruby allows it:

```ruby
def foo(array)
  value = if array.empty?
    0
  else
    return 3 # ✅ returns from function
  end
  # do stuff with value
end

```

Kotlin allows it:

```swift
fun foo(list: Collection<Int>): Int {
  var value = if (list.isEmpty()) {
    0
  } else {
    return 3 // ✅ returns from function
  }
  // do stuff with value
}

```

Scala allows it:

```swift
def foo(list: List[Int]): Int = {
  var value = if (list.isEmpty) {
    0
  } else {
    return 3 // ✅ returns from function
  }
  // do stuff with value
}

```

Javascript, C, C++, Dart, and PHP do not have conditional expressions (AFAICT); they only have the ternary operator.

Go does not even have the ternary operator! Assigning all branches to a local variable [is the idiomatic approach](https://stackoverflow.com/questions/19979178/what-is-the-idiomatic-go-equivalent-of-cs-ternary-operator).

Python at first seems to impose a limitation like Nate’s proposal. However, Python’s ` ___if___ else ___` expressions are really just its version of the ternary operator: its `if` expressions have an entirely different syntax from `if` statements, and do not support multiple statements. Python does not have the `?` `:` spelling of the ternary operator. (Ceylon is similar.)

The closest thing I could find to a clear precedent for Nate’s proposed rule is Java’s switch expressions:

```java
int value = switch (list.isEmpty()) {
    case true -> 0;
    case false -> return 3; // ❌ error: return outside of enclosing switch statement
};

```

I don't have a convenient C# dev environment handy. It would be interesting to check whether it supports `int x = boolValue switch { true => 0, false => return 3 };`. It seems likely it follows Java’s precedent.

---

_[View the full topic](https://forums.swift.org/t/se-0380-if-and-switch-expressions/61899)._
