Skip to content

listrophy/Inscribe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inscribe

As it turns out, Swift(describing:) is pretty slow, because it needs to demangle. It also results in an undefined value that you shouldn't be relying upon. There's a replacement (_typeName), but that's private API.

Enter @Inscribe:

enum Parent {
    @Inscribe public final class Widget: CustomStringConvertible {}
}

let w = Parent.Widget()

print("The Widget is described as \(w.description)")
// => "The Widget is described as Parent.Widget"

The @Inscribe macro will, in this case, expand into:

public extension Parent.Widget {
    var description: String {
        "Parent.Widget"
    }
    static var description: String {
        "Parent.Widget"
    }
}

Installation

Install with Swift Package Manager, do an import Inscribe, and start annotating your types!

Areas for improvement

This macro was initially written in about half an hour. Basically, the improvements needed are (in order):

  1. Access modifier is hardcoded to public. Instead, read the access modifier from the attached declaration.
  2. The expansion(...) body of this macro just uses the init(_ stringInterpolation: SyntaxNodeString) mechanism. Build up the AST from real types instead of an interpolated string.
  3. Constrain this to the types it should be constrained to.

Contributing

Contributions are welcome! Throw me a PR, and I'll take a look.

About

Swift macro to replace usage of String(describing:) with compile-time string

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages