Skip to content

CaptureContext/swift-result-builders

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swift-result-builders

SwiftPM 6.2 Platforms @capture_context

Products

Usage

ArrayBuilder

Create initializers for your own collections, here is an example of IdentifiedArray extension for swift-identified-collections

import IdentifiedCollections
import ArrayBuilder

extension IdentifiedArray {
  @inlinable
  public init(
    id: KeyPath<Element, ID>,
    @ArrayBuilder<Element> uniqueElements: () -> [Element]
  ) {
    self.init(id: id, uniqueElements: uniqueElements())
  }
}

extension IdentifiedArray where Element: Identifiable, ID == Element.ID {
  @inlinable
  public init(
    @ArrayBuilder<Element> uniqueElements: () -> [Element]
  ) {
    self.init(uniqueElements: uniqueElements())
  }
}

func example(all: Bool) {
  IdentifiedArray(id: \.self) {
    if all {
      "all"
    } else {
    	"some"
    }
    "values"
    "are"
    "here"
  }
}

Array initializer is provided by default

[1,2,3,4,5,6,7] == Array {
  1
  2
  [3, 4]
  [[5], [6, 7]]
}

See more examples in Tests

Installation

Basic

You can add swift-result-builders to an Xcode project by adding it as a package dependency.

  1. From the File menu, select Swift Packages › Add Package Dependency…
  2. Enter "https://github.com/capturecontext/swift-result-builders" into the package repository URL text field
  3. Choose products you need to link them to your project.

Recommended

If you use SwiftPM for your project structure, add DeclarativeConfiguration to your package file.

.package(
	url: "[email protected]:capturecontext/swift-result-builders.git",
	.upToNextMinor(from: "0.0.1")
)

or via HTTPS

.package(
	url: "https://github.com:capturecontext/swift-result-builders.git",
	.upToNextMinor(from: "0.0.1")
)

Do not forget about target dependencies:

.product(
	name: "ArrayBuilder",
	package: "swift-result-builders"
)

License

This library is released under the MIT license. See LICENSE for details.