-
Hello! I am having trouble migrating the following code to the new result builders syntax in Swift 5.8. The code uses a conversion in the parsing block. Minimal code sample: enum AppRoute: Equatable {
case route(
value: String? = nil,
subRoute: SubRoute? = nil
)
}
enum SubRoute: String {
case sub1
case sub2
}
// let router: some Router<AppRoute> = OneOf {
let router = OneOf {
Route(.case(AppRoute.route)) {
Parse(routeConversion) {
OneOf {
Path { "path1" }.map { SubRoute.sub1 }
Path { "path2" }.map { SubRoute.sub2 }
}
Always<URLRequestData, String?>(nil) // simplified for demo purpose
}
}
// .. other route cases
}
let routeConversion: AnyConversion<(SubRoute?, String?), (String?, SubRoute?)> = .convert(
apply: { subRoute, value in
(value, subRoute)
},
unapply: { value, subRoute in
(subRoute, value)
}
) The code did work fine in Xcode 14.2 with But it doesn't work in Xcode 14.3 with
However changing the What is the right way to fix this error? Is there another way to explicitly specify the types in the parsing block when using a conversion? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
@stephencelis You mentioned this in the swift-parsing release announcement thread:
Could this also be the case here? Do you have a tip how to specify the type more explicitly or do you have another idea how to fix this? |
Beta Was this translation helpful? Give feedback.
-
I wonder if your conversion trouble isn't a downstream consequence of incorrectly modeled routes. You've modeled I tried to have a go at solving your compiler error, but because of the oddly modeled routes, I can't even quite make out what routes you intend to model. I feel like if you clarified your route modeling, you wouldn't need to do that ad-hoc conversion between |
Beta Was this translation helpful? Give feedback.
@kamcma Thank you for looking into this. Regarding the sub route behavior, I just named it
SubRoute
for demo purposes, it is actually more a configuration value derived from the path or query parameters.Fortunately, I figured out the solution myself by having a closer look at the error message again:
Initializer 'init(_:with:)' requires the types '(String?, SubRoute?)' and '(String?, SubRoute)' be equivalent
SubRoute?
) is optional because it is modelled as optional in the enumSubRoute
) being non optional because this is inferred from the parsing block: