# \[FEATURE REQUEST\] \`guard\` statement

**URL:** <https://forums.unrealengine.com/t/feature-request-guard-statement/1162211>\
**Category:** Verse\
**Tags:** unreal-editor-for-fortnite, fortnite-creative, fortnite\
**Created:** [May 11, 2023, 6:44am UTC](https://forums.unrealengine.com/t/feature-request-guard-statement/1162211 "2023-05-11T06:44:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Knight\_Breaker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/knight_breaker/32/1194711_2.png) [@Knight\_Breaker](https://forums.unrealengine.com/u/Knight_Breaker)\
**Post date:** [May 11, 2023, 6:44am UTC](https://forums.unrealengine.com/t/feature-request-guard-statement/1162211/1 "2023-05-11T06:44:19Z")

</div>

Verse shipped with an `if` statement / expression, but it’s quickly becomes apparent that the languages often times suffers from the great pyramid of doom phenomenon.

```auto
if (...) {
  if (...) {
    if(...) {
      ...
    }
  }
}

```

Being used to Swift development I fell in love with the `guard` statement.

> Guard Statement
> 
> A `guard` statement is used to transfer program control out of a scope if one or more conditions aren’t met.
> 
> A `guard` statement has the following form:
> 
> ```swift
> guard <#condition#> else {
> <#statements#>
> }
> 
> ```
> 
> The value of any condition in a `guard` statement must be of type `Bool` or a type bridged to `Bool`. The condition can also be an optional binding declaration, as discussed in [Optional Binding](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics#Optional-Binding).
> 
> Any constants or variables assigned a value from an optional binding declaration in a `guard`statement condition can be used for the rest of the guard statement’s enclosing scope.
> 
> The `else` clause of a `guard` statement is required, and must either call a function with the `Never` return type or transfer program control outside the guard statement’s enclosing scope using one of the following statements:
> 
> - `return`
> - `break`
> - `continue`
> - `throw`
> 
> [Source](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/statements/#Guard-Statement)

The guard statement allows one to inverse some control flow and keep the execution within the same scope level instead of moving into a new `then` branch.

Here’s a quick example from Swift:

```swift
func foo() -> Int? {
  if condition {
    if let unwrappedInt = someOptionalInt {
      if otherCondition {
        doSomeWork()
        return unwrappedInt
      }
    }
  }
  cleanUpWork()
  return nil
}

func foo() -> Int? {
  guard condition, let unwrappedInt = someOptionalInt, otherCondition else {
    cleanUpWork()
    return nil
  }
  // we escaped the if pyramid
  doSomeWork()
  return unwrappedInt
}

```

This example is not a question if the code can be restructured to return an int or fail, it’s just a trivial example of a possible `if` pyramid of doom!

I can partly achieve something similar for some use case in verse, but it’s a bit verbose.

```auto
Func(): void = {
  Value := if (UnwrappedValue := OptionalValue?) { 
    UnwrappedValue 
  } else {
    return
  }

  Something := if (UnwrappedThing := OptionalThing?) { 
    UnwrappedThing 
  } else {
    return
  }

  # use `Value` and `Something` from here
}

```

It would be great if verse gained a native solution for a guard statement (and possibly an expression?).

---

<div class="post-metadata">

**Author:** ![Conan\_Reis](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/conan_reis/32/1016955_2.png) [@Conan\_Reis](https://forums.unrealengine.com/u/Conan_Reis)\
**Post date:** [May 11, 2023, 7:25am UTC](https://forums.unrealengine.com/t/feature-request-guard-statement/1162211/2 "2023-05-11T07:25:58Z")

</div>

We’ve discussed a `guard` expression for years internally at Epic and the consensus was generally to see what the community thought about it - so I’m glad you bring it up.

The more examples pyramids of doom that the community can supply of real and not just pseudo code - the better the case for it.

---

<div class="post-metadata">

**Author:** ![Knight\_Breaker](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/knight_breaker/32/1194711_2.png) [@Knight\_Breaker](https://forums.unrealengine.com/u/Knight_Breaker)\
**Post date:** [May 11, 2023, 7:33am UTC](https://forums.unrealengine.com/t/feature-request-guard-statement/1162211/3 "2023-05-11T07:33:22Z")

</div>

Happy to see that it’s been discussed internally. In my personal experience so far I often times have to restructure my code an split it a lot of small methods that partly swallow the nested if statements so that it does not grow the pyramid too quickly. In many cases this involves dynamic type casting, optional unwrapping etc. (shifting between `agent`, `player`, `fort_character`, grabbing and unwrapping optionally cached stored properties or accessing `array`s and `map`s).

I’m not sure if the majority from the creators in the Fortnite community is familiar with this or if they just assume that deeply nested if statements is the status quo in modern programming languages. Let’s hope there are more experienced developers who would share their need for a native `guard` statement for Verse.

---

<div class="post-metadata">

**Author:** ![Tim\_Sweeney](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tim\_Sweeney](https://forums.unrealengine.com/u/Tim_Sweeney)\
**Post date:** [May 13, 2023, 9:37pm UTC](https://forums.unrealengine.com/t/feature-request-guard-statement/1162211/4 "2023-05-13T21:37:41Z")

</div>

Swift’s guard statement introduces a new control flow paradigm to the mix. In Verse, there are two idioms that I think are a bit simpler and more general. An ‘if’ and ‘for’ statement’s condition may have an arbitrary number of variable bindings and tests separated by commas, semicolons, or newlines, and can be indented.

if(a):  
if(b):  
if(c):  
.

can be written as…

if:  
a  
b  
c  
then:  
something using any variables bound in a,b,c  
else:  
something else

Also, if you’re writing a function, and your guard’s ‘else’ just fails, you can skip the ‘if’ statements and just intermix your tests, variable bindings, and code together sequentially.
