Write Swift macros

RSS for tag

Discuss the WWDC23 Session Write Swift macros

Posts under wwdc2023-10166 tag

8 Posts
Sort by:

Post

Replies

Boosts

Views

Activity

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
426
Sep ’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
1k
Oct ’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
441
Sep ’23
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.4k
Jun ’24
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
712
Oct ’23
How to convert a String from a macro to a value that can be used in the implementation of this plugin.
I'm trying to make a macro that has the following structure: @SampleDataProviding(decoder: JSONDecoder(), bundlePath: Bundle.module.bundlePath, fileExtension: "json") The primary issue that I am having is that I need to be able to scan a subdirectory of the bundlePath for files that match a certain criteria. However, when I try to access the Bundle.module.bundlePath it literally spits back at me "Bundle.module.bundlePath" which obviously doesn't get me anywhere. I've looked all over for a way to convert what I enter at the macro call site to an actual path that I can put in code that isn't generated. (This macro goes through a specific subdirectory looking for sample data files which it then generates a variable expression for each of them so that it is easy to access this sample data (for UI work or testing).) Each time I try this it fails stating that the folder doesn't exist because it's looking for "Bundle.module.bundlePath/[subdirectory]" when I want it to look for the subdirectory in the bundle path, not "Bundle.module.bundlePath". I've been able to get this to work in testing by wrapping the call in an interpolation from a string but this does not work in the real world because the compiler complains that the interpolation cannot be modified (it is also more clunky, I would prefer to avoid having to wrap the entire thing in an interpolation).
2
0
622
Aug ’23
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.0k
Dec ’23