Write Swift macros

RSS for tag

Discuss the WWDC23 Session Write Swift macros

Posts under wwdc2023-10166 tag

7 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

External macro implementation type could not be found for macro 'stringify'
Hi. I created a new swift macro package and wan't to start using the template which contains the #stringify macro out of the box without any changes. The package itself runs incl. tests But now I added the Package to a dummy iOS app project / workspace and I can't compile if im using this code let (_, _) = #stringify(a + b) + package import Compile error: External macro implementation type '***.StringifyMacro' could not be found for macro 'stringify' I am using Xcode 15 and macOS 13.5.2 Restart Xcode, Clean etc doesn't help. Can anyone help my ? Thanks :)
4
1
2.6k
Jun ’24
Swift Macro: Missing package product 'MyMacroApple'
In Xcode 15.0.0 I have created a package using a template Swift Macro. I have named it 'MyMacroApple'. The template comes with #stringify macro so I have created an new app and named it 'MyMacroApp' then copy pasted the code from 'MyMacroApple' main.swift file import MyMacroApple let a = 17 let b = 25 let (result, code) = #stringify(a + b) then I have added Local package dependency to the app project and selected package product 'MyMacroApple' of Library kind to my 'pocMyApp' target. When I run the project I get the error: "No such module 'MyMacroApple'"
6
0
3.2k
Dec ’23
OSLog in Swift macro doesn't persist original file/line number
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).
5
0
1.1k
Oct ’23
How to use a freestanding macro with OSLog
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.
2
0
774
Oct ’23
Macros that creates String.LocalizationValue
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
1
0
460
Sep ’23
Do #freestanding macros generate code before @attached?
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?
0
0
476
Sep ’23