SwiftUsing brings to Swift the using
feature from Jai, Jonathan Blow's programming language. It uses SwiftSyntax to code generate the required code.
To learn more about SwiftUsing development you can check out the blog post Implementing using in Swift with SwiftSyntax.
using
helps with Struct composition.
It allows you to recover the nice syntax from class inheritance when you use struct
s instead.
struct User {
var name: String
var age: Int
}
struct Friend {
// using
let user: User
var friendshipDate: Date
}
friend.name // with class inheritance
friend.user.name // with struct composition :(
friend.name // with struct composition and using !!
- Annotate a property with a comment:
// using
struct Friend {
...
// using <- add this comment
let user: User
...
}
Note that both types must be in the same file. See Known limitations below.
- Run the command line tool
swiftusing /path/to/file
Clone the repo and run make install
.
- Basic declaration syntax
let/var identifier: Type
- Reliant on comments (is just an external tool after all)
- Single file, using types must be in the same file.
- No collision detection, the compiler will error instead.
## Acknowledgements
- Jonathan Blow for sharing the development of Jai
- pointfree for the informative and inspiring videos
- Swift AST Explorer
- swift-syntax obviously!
- Yaap
- PackageBuilder
SwiftUsing is developed completely in the open, and your contributions are more than welcome.
This project does not come with GitHub Issues-based support, and users are instead encouraged to become active participants in its continued development — by fixing any bugs that they encounter, or improving the documentation wherever it’s found to be lacking.
If you wish to make a change, open a Pull Request — even if it just contains a draft of the changes you’re planning, or a test that reproduces an issue — and we can discuss it further from there.
Alejandro Martinez | http://alejandromp.com | @alexito4