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
.
In your macro or wherever you are using SwiftSyntax
:
import LiteralsMapper
// ...
print(someExprSyntax.inferedType)
inferedType
is defined on the following types
ExprSyntax
IntegerLiteralExprSyntax
FloatLiteralExprSyntax
StringLiteralExprSyntax
BooleanLiteralExprSyntax
RegexLiteralExprSyntax
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.
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
]
)
The minimum Swift version supported by swiftui-frameless-window releases are detailed below:
swiftui-frameless-window | Minimum Swift Version |
---|---|
0.1.0 ... |
5.9 |