How do result builders handle if/else-if, without plain else, chains?

As I understand it for Result Builders,

  • a lone if statement maps to a build-optional
  • an if-else pair maps to two build-either calls
  • a switch maps to a tree of build-either calls
  • an if/(else-if)/else chain also maps to a tree of build-either calls

So what happens for if/(else-if) chain with no plain-else case? A bunch of build-either ending with a build-optional?

2 Likes

Depending on what you mean by "maps", yes! :wink: (You need both buildEithers, for else to compile, but they don't actually get called unless necessary.)

@resultBuilder enum ResultBuilder {
  static func buildEither(first: some Any) -> Never { fatalError() }
  static func buildEither(second: some Any) -> Never { fatalError() }
  static func buildOptional(_: Never?) -> Int { 1 }
  static func buildBlock(_ value: Int) -> Int { value }
}
@ResultBuilder var value: Int {
  if false { 0 } else if false { 0 } 
}
#expect(value == 1)
1 Like