Skip to content

Instantly share code, notes, and snippets.

@noahsark769
Created January 8, 2021 17:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahsark769/7c34ff4132513d14a6db107ada505fc9 to your computer and use it in GitHub Desktop.
Save noahsark769/7c34ff4132513d14a6db107ada505fc9 to your computer and use it in GitHub Desktop.
SwitchExpression.swift
@_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