I have a macro with following definition:
@attached(peer)
@available(swift 5.9)
public macro CodedBy<Parent, Helper: HelperCoder, each Argument, each Property>(
_ helperAction: (repeat each Argument, repeat each Property) -> Helper,
arguments: repeat each Argument,
properties: repeat KeyPath<Parent, each Property>
) = #externalMacro(module: "MacroPlugin", type: "CodedBy")
When I try to use this macro with the following syntax ` @CodedBy(Image.IdentifierCoder.init, arguments: "text", properties: \ListItem.id)`
@Codable
struct ListItem: Identifiable {
let title: String
@CodedBy(Image.IdentifierCoder.init, arguments: "text", properties: \ListItem.id)
let image: Image
let id: String
@Codable
struct Image: Identifiable {
var id: String { identifier }
@IgnoreCoding
private(set) var identifier: String!
let width: Int
let height: Int
struct IdentifierCoder: HelperCoder {
let text: String
let id: String
func decode(from decoder: any Decoder) throws -> Image {
var image = try Image(from: decoder)
image.identifier = id
return image
}
func encode(_ value: Image, to encoder: any Encoder) throws
{
var image = value
image.identifier = nil
try image.encode(to: encoder)
}
}
}
}
I get the following errors:
Cannot convert value of type '@Sendable (String, String) -> ListItem.Image.IdentifierCoder' to expected argument type '(String, String) -> Helper'
Generic parameter 'Helper' could not be inferred
Why isn't compiler able to refer generic parameter Helper
is ListItem.Image.IdentifierCoder
?
This can be tested out by adding the code to MetaCodableTests
test target in this branch.