Swiftpack.co - omochi/SwiftTypeReader as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by omochi.
omochi/SwiftTypeReader 2.5.1
You can gather type definitions from Swift source code.
⭐️ 23
🕓 4 weeks ago
.package(url: "https://github.com/omochi/SwiftTypeReader.git", from: "2.5.1")


You can gather type definitions from Swift source code.


    func testReadmeExample() throws {
        try withExtendedLifetime(Context()) { (context) in
            let module = context.getOrCreateModule(name: "main")
            let reader = Reader(context: context, module: module)

            let source = try reader.read(
                source: """
struct S {
    var a: Int?
                file: URL(fileURLWithPath: "S.swift")
            _ = source

            let s = try XCTUnwrap(module.find(name: "S")?.asStruct)
            XCTAssertEqual(s.name, "S")

            XCTAssertEqual(s.storedProperties.count, 1)
            let a = try XCTUnwrap(s.find(name: "a")?.asVar)
            XCTAssertIdentical(a, s.storedProperties[safe: 0])
            XCTAssertEqual(a.name, "a")

            let aType = try XCTUnwrap(a.interfaceType.asEnum)
            XCTAssertEqual(aType.name, "Optional")
            XCTAssertEqual(aType.genericArgs.count, 1)

            let aWrappedType = try XCTUnwrap(aType.genericArgs[safe: 0]?.asStruct)
            XCTAssertEqual(aWrappedType.name, "Int")

More documents

Unsupported language features

Function body

It handles only signature.

Generic signatures of function

Variable without type annotation

struct S {
    var a = 0

It doesn't have type inference.


This library focus to use for building other libraries below.

But It's useful in standalone for other purpose like meta programming for Swift.


Design consideration

This library refer to the Swift compiler and Slava's book to build architecture. It provides decls, types, and type reprs.

Code generation

$ swift package codegen


Stars: 23
Last commit: 4 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

4 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics