What is TypeName.Type and TypeName.self and how do they relate?

I'm trying to write a Macro, get used to it. I'm facing sme problems and I realize I don't really understand What something like String.Type and String.self means.

Was wondering if someone could explain things and help me understand why I'm facing this error (or maybe I can do it myself after reading the explanation):

P.S. The whole cast might seem odd to you, the reason I'm doing it is because using po node or any other lldb command in the debugger will not yield anything, node is not recognized, not even with frame variable show.

TypeName.Type is the TYPE of TypeName.self

Just like in let typeName = TypeName() we say TypeName is the TYPE of typeName.

MetaType vs Type level

Firstly, if you're trying to print a variable that's generic using LLVM debugging, a reliable workaround is to replace generics written as some with a generic parameter clause within the function, as demonstrated below:

-public static func expansion(
-  of node: some FreestandingMacroExpansionSyntax,
-  in context: some MacroExpansionContext
 ) -> ExprSyntax {
+public static func expansion<Node: FreestandingMacroExpansionSyntax, Context: MacroExpansionContext>(
+  of node: Node,
+  in context: Context
 ) -> ExprSyntax {

Regarding the error you encountered, Type 'any FreestandingMacroExpansionSyntax' cannot conform to 'SyntaxProtocol', it arises when you attempt to pass a protocol where a type is expected. To cast the node to a protocol, consider using SyntaxProtocol.asProtocol(_:).

1 Like