Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

πŸ•΅πŸ»β€β™‚οΈ Simple type inference for literals in SwiftSyntax.

License

Notifications You must be signed in to change notification settings

astzweig/swift-syntax-literals

Repository files navigation

SwiftSyntax Literals

Warning Archived on Aug 04, 2023. The same functionality can be achived through generics. See the Retrieve type of initializer expression in accessor macro post on forums.swift.org for more informations.

Extends SwiftSyntax's literal expression types with a computed property named inferedType, that returns the default infered type of a literal. This is mostly useful for attached macros that act like property wrappers and want to offer users the convenience of leaving out type annotations. E.g. in the following case:

@UserDefault var luminosity = 5

which expands to

var luminosity {
  get {
     ...
     let defaultValue = UserDefaults.standard.object(forKey: "luminosity") as? Int
     return defaultValue ?? 5
  }
}

the UserDefault macro needs to know the type of luminosity in order to cast the result of UserDefaults.standard.object(forKey:). Without a simple type inference the user would have been obligated to annotate the type of luminosity.

Usage

In your macro or wherever you are using SwiftSyntax:

import LiteralsMapper
// ...
print(someExprSyntax.inferedType)

Conforming simple types

inferedType is defined on the following types

  • ExprSyntax
  • IntegerLiteralExprSyntax
  • FloatLiteralExprSyntax
  • StringLiteralExprSyntax
  • BooleanLiteralExprSyntax
  • RegexLiteralExprSyntax

Conforming complex types

Additionally inferedType is defined on ArrayExprSyntax and DictionaryExprSyntax. In this case it yields a value only if the structure contains conforming simple types as specified above, or are nested structures of them.

Adding swift-syntax-literals as a dependency

To use the LiteralsMapper library in a Swift project, add it to the dependencies for your package:

let package = Package(
    // name, platforms, products, etc.
    dependencies: [
        // other dependencies
        .package(url: "https://github.com/astzweig/swift-syntax-literals", from: "0.1.0"),
    ],
    targets: [
        .macro(name: "<macro-name>", dependencies: [
            // other dependencies
            .product(name: "LiteralsMapper", package: "swift-syntax-literals"),
        ]),
        // other targets
    ]
)

Supported Versions

The minimum Swift version supported by swiftui-frameless-window releases are detailed below:

swiftui-frameless-window Minimum Swift Version
0.1.0 ... 5.9

About

πŸ•΅πŸ»β€β™‚οΈ Simple type inference for literals in SwiftSyntax.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages