Expand on Swift macros

RSS for tag

Discuss the WWDC23 session Expand on Swift macros

View Session

Posts under wwdc2023-10167 tag

6 Posts
Sort by:
Post not yet marked as solved
5 Replies
745 Views
I need to log to OSLog and into a file in parallel (due to OSLogStore not being able to provide old logs (FB13191608)). In Objective-C I can accomplish this with a macro using __FILE__ and __LINE__ in the macro implementation which still reference the position in the original code. I tried to create a Swift macro to make this work from Swift. However, log() takes the file and line number of the macro definition file instead of the position in the calling code. So when I click on the metadata link, I'm taken to the macro instead of the calling location. Is there any solution to that? #file and #line are correctly set in the macro but there’s no way to specify file and line number to the log() function (FB13204310).
Posted
by ortwin.
Last updated
.
Post not yet marked as solved
2 Replies
550 Views
I'm trying to create a macro that adds the file and line to a string that I use with OSLog. However, I only get warnings that either the String is not in the format of OSLogMessage or not an interpolated string. The Macro looks like this at the moment: public struct LocMacro: ExpressionMacro { public static func expansion( of node: some FreestandingMacroExpansionSyntax, in context: some MacroExpansionContext ) -> ExprSyntax { guard let argument = node.argumentList.first?.expression.as(StringLiteralExprSyntax.self)?.segments else { fatalError("compiler bug: the macro does not have any StringLiteralExprSyntax arguments.") } guard let file = context.location(of: node)?.file.as(StringLiteralExprSyntax.self)?.segments, let line = context.location(of: node)?.line.as(IntegerLiteralExprSyntax.self) else { fatalError("compiler bug: the macro is unable to retrieve file and line numbers") } return "\"\(argument) - \(file):\(line)\"" } } and here the exposed macro: @freestanding(expression) public macro loc(_ text: String) -> String = #externalMacro(module: "LxoMacrosMacros", type: "LocMacro") I want to use it like this: import LxoMacros import OSLog let logger = Logger() let someNumber = 17 logger.debug(#loc("Working with some number \(someNumber)")) which should expand to: logger.debug("Working with some number \(someNumber) - MyFile.swift:8") Is there a way to change the macro so that it returns the correct type? When expending the macro it does show the right string, which works with print() and so on, but assuming since the Logger uses some sort of compiler check as well, it doesn't seem to work together as expected.
Posted Last updated
.
Post not yet marked as solved
1 Replies
316 Views
Hey guys, I have developed successfully a Macro that creates enum cased descriptions of type "String.LocalizationValue". Using the variable of these created ones and "String(localized: theVariable)" doesn't include them into my string catalog. So the whole point why I did failed at the last point. Can someone explain me if this will come or get fixed somewhere near in the future? thanks
Posted
by Bo0o0om.
Last updated
.
Post not yet marked as solved
0 Replies
346 Views
I wrote an @attached macro for generating an init. However, I also use this same class for previews. I get an error that it's impossible to create an instance of the class because the init that was supposed to be generated by the macro is missing. It seems that the #freestanding macro executes first, and at that moment, the code generated by @attached is not yet present. Is there a way to fix this?
Posted
by Horoko.
Last updated
.
Post not yet marked as solved
0 Replies
419 Views
Hi, I thought I'd ask as this sounds like the perfect use for Swift Macros and I'm betting other people ran into the same issue :) I'm currently trying to simplify [weak self] captures in some of my code. For example: // Usual syntax someAlert.addTextField { [weak self] textField in guard let self else { return } textField.text = somethingInSelf() } Adding a macro seems to trade one kind of wordiness for another: // Using a macro works, but is wordy because it requires non-trailing syntax someAlert.addTextField(configurationHandler: #weakSelf { textField in textField.text = somethingInSelf() }) Ideally I imagine there must be a way to do something like the code below: // Using the same macro fails when applied to a trailing closure someAlert.addTextField #weakSelf { textField in textField.text = somethingInSelf() } Does anyone have any suggestions? I feel this is probably a known/solved problem and I'm being a bit slow on the uptake :D
Posted Last updated
.
Post marked as solved
2 Replies
627 Views
Hi, I've been playing around with macros and I found myself in a situation where I got attached accessor macro with 2 parameters - url and method. Both are strings. It looks like this public struct MyMacroMacro: AccessorMacro { public static func expansion<Context, Declaration>( of node: AttributeSyntax, providingAccessorsOf declaration: Declaration, in context: Context ) throws -> [AccessorDeclSyntax] where Context : MacroExpansionContext, Declaration : DeclSyntaxProtocol { guard let expressions = node.argument?.as(TupleExprElementListSyntax.self) else { //TODO throw error return [] } let segments = expressions.compactMap { $0.expression.as(StringLiteralExprSyntax.self)?.segments } let params = segments.compactMap { $0.trimmedDescription } guard params.count == 2 else { //TODO throw error return [] } return [ """ .init(url: url + "\(raw: params[0])", method: "\(raw: params[1])") """ ] } } Now, when I call it like: struct MyStruct { let url = "https://apple.com" @MyMacro(url: "/test", method: "some") var myVar: MyCustomType it produces something like this: struct MyStruct { let url = "https://apple.com" var myVar: MyCustomType { .init(url: url + " /test", method: " some") } I have no idea why it adds those whitespaces. When debugging, before calling "return" there are no whitespaces in "/test" or "some". Also - is there a way to create a macro that'll call another macro? For example MySecondMacro(url: "/url") to call MyMacro(url: "/url", method: "second")?
Posted
by neoth.
Last updated
.