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
You can add swift-result-builders to an Xcode project by adding it as a package dependency.
- From the File menu, select Swift Packages › Add Package Dependency…
- Enter
"https://github.com/capturecontext/swift-result-builders"
into the package repository URL text field - Choose products you need to link them to your project.
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"
)
This library is released under the MIT license. See LICENSE for details.