-3

In Apple's WWDC 2023 they announced macros and, among others, they showed us this code snippet:

macro unwrap<Wrapped>(_ expr: Wrapped, message: String) -> Wrapped

I have downloaded Xcode 15 Beta and tried to get it to work, without result. I get the error message of:

Macro 'unwrap(_:message:)' requires a definition

What can I do or how can I write this correctly?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vollan
  • 1,887
  • 11
  • 26

2 Answers2

1

You should create a new macro.In Xcode File → New → Package... and select Swift Macro

  • Hey and welcome, i noticed that after a while. Thank you for your answer though. Now i have finally built a lot of cool macros :) – Vollan Jun 27 '23 at 06:02
0

I haven't found your code snippet, but instead take a look at the video of WWDC, Write Swift macros, 06:36.

When creating a macro you need to declare the definition of your macro... For example in the video...

We have the macro

let (result, code) = #stringify(a+b)

To dive into the definition of #stringify...

// A macro that produces both a value and a string containing
// the source code that generated the value

@freestanding(expression)
public macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "WWDCMacros", type: "StringifyMacro")

So I guess that you are just missing the correct implementation. Do you have a implementation file of your macro?

Does it have the correct initializer?

// Check the overview here:
@freestanding(expression) // Creates a piece of code that returns a value
@freestanding(declaration) // Creates one or more declarations
@attached(peer) // Adds new declarations alongside the declaration its applied to
@attached(accessor) // Adds accessors to a property
@attached(memberAttribute) // Adds new declarations inside the type/extension its applied to
@attached(member) // Adds new declarations inside the type/extension its applied to
@attached(conformance) // Adds conformances to the type/extension its applied to
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Iskandir
  • 937
  • 1
  • 9
  • 21