You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Macros ... can be overloaded in the same way as functions, so long as the argument labels, parameter types, or result type differ.
I am overloading a macro to swap macro implementations based on a protocol conformance.
i.e. if MyType conforms to MyProtocol, expand to this, otherwise expand to that.
For example, we can modify #stringify to require a CustomStringConvertible argument
@freestanding(expression)public macro stringify<SomeStringConvertible:CustomStringConvertible>(_ value:SomeStringConvertible)->String= #externalMacro(module:"ExistentialOverloadMacros", type:"StringifyMacro")publicstructStringifyMacro:ExpressionMacro{publicstaticfunc expansion(of node:someFreestandingMacroExpansionSyntax, in context:someMacroExpansionContext)->ExprSyntax{
guard let argument = node.argumentList.first?.expression else{fatalError("compiler bug: the macro does not have any arguments")}return#""\(\#(argument))""#}}
Description
With a normal Swift function, a function accepting some CustomStringConvertible can implicitly unbox an argument of type any CustomStringConvertible.
This ability is described in SE-0352, Implicit Open Existentials
However, our modified #stringify cannot.
leta=17letb=25func printOut(_ value:someCustomStringConvertible){print("The value was '\(value)'")}letconvertible:anyCustomStringConvertible= a + b
printOut(convertible)// okprint("The value was \"\(#stringify(a + b))\"")// okprint("The value was \"\(#stringify(convertible))\"")// Error: Type 'any CustomStringConvertible' cannot conform to 'CustomStringConvertible'
Proposed solution
Similar to Swift functions, I would like macros with an argument of type some Protocol to accept a value of type any Protocol.
Alternatives considered
No response
Additional information
No response
The text was updated successfully, but these errors were encountered:
Motivation
SE-0382 states
I am overloading a macro to swap macro implementations based on a protocol conformance.
i.e. if MyType conforms to MyProtocol, expand to this, otherwise expand to that.
For example, we can modify
#stringify
to require aCustomStringConvertible
argumentDescription
With a normal Swift function, a function accepting
some CustomStringConvertible
can implicitly unbox an argument of typeany CustomStringConvertible
.This ability is described in SE-0352, Implicit Open Existentials
However, our modified
#stringify
cannot.Proposed solution
Similar to Swift functions, I would like macros with an argument of type
some Protocol
to accept a value of typeany Protocol
.Alternatives considered
No response
Additional information
No response
The text was updated successfully, but these errors were encountered: