# Declarative package description for SwiftPM using Function Builders

**URL:** <https://forums.swift.org/t/declarative-package-description-for-swiftpm-using-function-builders/28699>\
**Category:** Package Manager\
**Created:** [September 10, 2019, 2:24am UTC](https://forums.swift.org/t/declarative-package-description-for-swiftpm-using-function-builders/28699 "2019-09-10T02:24:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yusuke](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/yusuke/32/8962_2.png) [@yusuke](https://forums.swift.org/u/yusuke)\
**Post date:** [September 10, 2019, 2:24am UTC](https://forums.swift.org/t/declarative-package-description-for-swiftpm-using-function-builders/28699/1 "2019-09-10T02:24:00Z")

</div>

**Motivation**

[Function builders](https://forums.swift.org/t/function-builders/25167) allow us to have declarative interface. Declarative interface also gives you simpler and less source code. SwiftUI is a good example. It allows you to have declarative syntax to build view hierarchy very easily.

Currently, SwiftPM package description provides static func to build the description, but we might have alternative way using function builder here.

**Function Builders**

Below is an example of current package description. It provides convenient static func, so I think it's good enough interface.

```swift
let package = Package(
    name: "MyLibrary",
    platforms: [
        .macOS(.v10_14),
    ],
    products: [
        .library(name: "MyLibrary", targets: ["MyLibrary"]),
    ],
    dependencies: [
        .package(url: "https://url/of/another/package/named/Utility", from: "1.0.0"),
    ],
    targets: [
        .target(name: "MyLibrary", dependencies: ["Utility"]),
        .testTarget(name: "MyLibraryTests", dependencies: ["MyLibrary"]),
    ]
)

```

Below is an example of new approach using function builder. It can be more declarative than current one.

```swift
let package = Package(name: "Paper") {
    ProductList {
        Product(name: "tool", type: .static) {
            TargetList {
                Target(name: "tool")
            }
        }
    }
    DependencyList {
        Dependency(
            url: "http://example.com.com/ExamplePackage/ExamplePackage",
            from: "1.2.3"
        )
        Dependency(url: "dev", exact: "1.2.3")
    }
    TargetList {
        Target(name: "tool") {
            TargetDependencyList {
                TargetDependency(name: "Paper")
                TargetDependency(name: "ExamplePackage")
            }
        }
        Target(name: "Paper") {
            TargetDependencyList {
                TargetDependency(name: "Basic")
                Target(name: "Utility")
                Product(name: "AnotherExamplePackage")
            }
        }
    }
}

```

However, I think this approach has some downsides. So I would like to hear your opinions.

Pros

- Declarative description

Cons

- Can't have type constraints for all of description contents unless we defined function builder for specific types
- Compiler error is not friendly when we have wrong content at unexpected place

The only concern I have for function builder is that it might not work with package description well because it has type constraints in the tree structure. For example, it's not allowed to have Target on top level. We need to think if it should get compiler error or be ignored for encoded data.

**Data mapping**

As long as each description content conforms to Encodable, it can be mapped with current data types.

Here is [my rough implementation of the functionality](https://gist.github.com/kitasuke/ee73934c2eda0f63bbd371b69380c443). With this, we don't need to modify current data types.

---

<div class="post-metadata">

**Author:** ![jack](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jack/32/20250_2.png) [@jack](https://forums.swift.org/u/jack)\
**Post date:** [September 10, 2019, 7:22am UTC](https://forums.swift.org/t/declarative-package-description-for-swiftpm-using-function-builders/28699/2 "2019-09-10T07:22:33Z")

</div>

I'm -0.5 on this; for me the benefits do not outweigh the drawbacks. In addition to the stated cons, you'd lose context necessary for autocomplete to give good suggestions which hurts API discoverability. Also FWIW the existing package description API is already "declarative".

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [September 10, 2019, 11:28am UTC](https://forums.swift.org/t/declarative-package-description-for-swiftpm-using-function-builders/28699/3 "2019-09-10T11:28:03Z")

</div>

We should wait, function builder is not even part of the language yet.

---

<div class="post-metadata">

**Author:** ![yusuke](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/yusuke/32/8962_2.png) [@yusuke](https://forums.swift.org/u/yusuke)\
**Post date:** [September 11, 2019, 6:31am UTC](https://forums.swift.org/t/declarative-package-description-for-swiftpm-using-function-builders/28699/4 "2019-09-11T06:31:52Z")

</div>

Thank you for your opinion!  
I figured out that true benefit of function builders is define-and-run idea which means all of different types can be treated as if it's same at compile time, but they behave differently at run time. Declarative interface is not the biggest motivation to be used.  
I don't see the need in SwiftPM description, so I'm kind of thumbs down now...
