Utilities is a Swift library designed to provide more features that I feel that the language currently lacks. It allows developers to use features that are readily available in other languages, for example the Variant type this package provides could be likened to the std::variant of c++ language.
- Variant
- Tuple
- MaybeUninit
- Functions
And many more to be added later such as
- Isolated Functions
- Sendable Functions
To use this package
First, add the following package dependency to your package.swift file
.package(url: "https://github.com/Genaro-Chris/SwiftUtils", branch: "main")Then add the Utilities library to the target(s) you want to use it
.product(name: "Utilities", package: "SwiftUtils")For example to use the Variant construct
import Utilities
var variant = try Variant<String, Int, Double>(with: 12)
variant.visit(
{ (intValue: Int) in
print("Variant as an Int: \(intValue)")
},
{ (stringValue: String) in
print("Variant as an String: \(stringValue)")
},
{ (doubleValue: Double) in
print("Variant as an Double: \(doubleValue)")
}
)
try variant.interact(as: Int.self) {
$0 += 24
}
var oldValue = try variant.change(to: "Variant")Or using the MaybeUninit construct
var uninit = MaybeUninit<cpptype>() // cpptype is complex type that required further initiailization
uninit.unsafeInitialize { pointer in
cpptype_init(pointer)
}
// uninit is now fully initialized and can be used without fear of UB (Undefined Behaviour) from improper initialization
let cppResult = uninit.value.cpp_method(123)
// This method of zeroInitalized is similar to c initialization technique
// ctype_t val = ctype_t{0};
var anotherUninit = MaybeUninit<ctype_t>.zeroInitialize() // ctype_t is simple type that can be zero initiailized
let cResult = ctype_method(&anotherUninit.value, 93)I highly welcome and encourage all sorts of contributions from all swift developers. Feel free to also star the repository if you like the project
If you like this project you can contribute it by:
- Submit a bug report by opening an issue
- Submit code by opening a pull request
I'm available for hire whether full time or part-time at the moment. Reach out to me at this email
This package is released under Apache-2.0 license. See LICENSE for more information.