Created
January 8, 2021 17:39
-
-
Save noahsark769/7c34ff4132513d14a6db107ada505fc9 to your computer and use it in GitHub Desktop.
SwitchExpression.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@_functionBuilder struct SwitchExpression { | |
static func buildBlock<T>(_ content: T) -> T { | |
return content | |
} | |
static func buildEither<T>(first: T) -> T { | |
return first | |
} | |
static func buildEither<T>(second: T) -> T { | |
return second | |
} | |
} | |
func switchExpression(@SwitchExpression _ content: () -> String) -> String { | |
return content() | |
} | |
enum MyEnum { | |
case one | |
case two | |
case three | |
} | |
// 1 | |
let enumValue1 = MyEnum.one | |
let computedValue1: String | |
switch enumValue1 { | |
case .one: computedValue1 = "one" | |
case .two: computedValue1 = "two" | |
case .three: computedValue1 = "three" | |
} | |
// 2 | |
let enumValue2 = MyEnum.two | |
let computedValue2 = switchExpression { | |
switch enumValue2 { | |
case .one: "Hey" | |
case .two: "What up" | |
case .three: "Yo" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment