Skip to content

[Compile Time Constant Extraction] Extract Result Builder Expressions #75219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

venkatesh5789
Copy link
Contributor

@venkatesh5789 venkatesh5789 commented Jul 12, 2024

Added extraction logic for Result Builder syntax. An example of a defined result builder is the following:

@resultBuilder
public enum FooBuilder {
    public typealias Component = [Foo]
    public typealias Expression = Foo

    public static func buildExpression(_ element: Expression) -> Component {
        return [element]
    }

    public static func buildOptional(_ component: Component?) -> Component {
        guard let component = component else {
            return []
        }
        return component
    }

    public static func buildEither(first component: Component) -> Component {
        return component
    }

    public static func buildEither(second component: Component) -> Component {
        return component
    }

    public static func buildArray(_ components: [Component]) -> Component {
        return Array(components.joined())
    }

    public static func buildBlock(_ components: Component...) -> Component {
        return components.flatMap { $0 }
    }
    
    public static func buildLimitedAvailability(_ component: Component) -> Component {
        return component
    }
}

An example usage is the following:

public struct MyFooProviderInferred: FooProvider {
    public static var foos: [Foo] {
        Foo(name: "AAAAAA")
        Foo(name: "BBBBB")

        if someNumber < 3 {
            Foo(name: "Less Than 3")
        } else if someNumber < 7 {
            Foo(name: "Between 3 and 6")
        } else {
            Foo(name: "Between 7 and 10")
        }

        for i in 1...3 {
            Foo(name: "OptionalArrayRange\(i)")
        }

        if (Bool.random()) {
            Foo(name: "Optional")
        }

        if #available(macOS 99, *) {
            Foo(name: "MacOS 99 1")
            Foo(name: "MacOS 99 2")
        } else {
            Foo(name: "Other MacOS")
        }
    }
}

This also supported nested builders. Something like:

public struct MyFooProviderInferred: FooProvider {
    public static var foos: [Foo] {
        Foo(name: "MyFooProviderInferred.foos.1", baz: {
            "Nested.Builder.1"
            "Nested.Builder.2"
        })
        Foo(name: "MyFooProviderInferred.foos.2")
  }
}

@venkatesh5789 venkatesh5789 force-pushed the result-builder-extraction branch 3 times, most recently from 10642d0 to 60760aa Compare July 15, 2024 22:59
@venkatesh5789
Copy link
Contributor Author

@swift-ci test

2 similar comments
@venkatesh5789
Copy link
Contributor Author

@swift-ci test

@venkatesh5789
Copy link
Contributor Author

@swift-ci test

@venkatesh5789 venkatesh5789 force-pushed the result-builder-extraction branch from 60760aa to 4c3439f Compare July 24, 2024 17:10
@venkatesh5789
Copy link
Contributor Author

@swift-ci test

2 similar comments
@venkatesh5789
Copy link
Contributor Author

@swift-ci test

@venkatesh5789
Copy link
Contributor Author

@swift-ci test

@venkatesh5789 venkatesh5789 enabled auto-merge July 25, 2024 01:00
@venkatesh5789 venkatesh5789 merged commit 82f85d8 into swiftlang:main Jul 25, 2024
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants