Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Consider using Result instead of optional response and error #414

Closed
SebastianOsinski opened this issue Feb 14, 2020 · 4 comments · Fixed by #418
Closed

Suggestion: Consider using Result instead of optional response and error #414

SebastianOsinski opened this issue Feb 14, 2020 · 4 comments · Fixed by #418
Labels
backwards incompatible changes that break backwards compatibility of public API op-ex Refactoring, Tech Debt or any other operational excellence work.
Milestone

Comments

@SebastianOsinski
Copy link
Contributor

This is merely a suggestion. We could use Result for completion handlers instead of all optional arguments:

public typealias RouteCompletionHandler = Result<([Waypoint], [Route]), DirectionsError> -> Void

public typealias MatchCompletionHandler = Result<[Match], DirectionsError> -> Void

This still can be done before 1.0.0 release. The only thing I am not sure about is [Waypoint] in RouteCompletionHandler. Is it always returned when routes are returned? Or is it possible to get waypoints but not routes or vice-versa?

@1ec5
Copy link
Contributor

1ec5 commented Feb 14, 2020

Thanks for the suggestion! This overlaps with some of the proposal in #391 to encapsulate Route/Match in a response object. That refactoring is underway in #406.

We’ve been going back and forth about whether the options object and error should be members of the response object or live alongside it in the completion handler. The optionality is unfortunate, given that a nontrivial response is mutually exclusive with an error. That might be a case for using an enumeration with associated values. 🤔

A generic response type would be an interesting idea. So far we’ve been thinking about a response protocol, which would enforce the presence of a few members we think would be common to any navigation-related Mapbox API. But a generic type might be a cleaner way to go about that.

Please have a look at #406. We’d welcome suggestions, though we also expect these types to require additional rounds of refactoring, such as for waypoints in #388.

The only thing I am not sure about is [Waypoint] in RouteCompletionHandler. Is it always returned when routes are returned? Or is it possible to get waypoints but not routes or vice-versa?

As things stand, the response objects would fall back to the waypoints in the options object:

} else {
waypoints = decodedWaypoints
}

However, certain errors will pass no waypoints to the completion handler, while others will pass in the response object’s waypoints:

DispatchQueue.main.async {
completionHandler(nil, nil, apiError)
}
DispatchQueue.main.async {
completionHandler(result.waypoints, nil, .unableToRoute)
}

/cc @JThramer

@1ec5 1ec5 added backwards incompatible changes that break backwards compatibility of public API op-ex Refactoring, Tech Debt or any other operational excellence work. labels Feb 14, 2020
@1ec5
Copy link
Contributor

1ec5 commented Feb 17, 2020

Oh whoops, I didn’t realize you were referring to the Result type in the standard library – that’s a good idea.

@1ec5 1ec5 added this to the v1.0 milestone Feb 17, 2020
@JThramer
Copy link
Contributor

Yeah this is a fantastic idea, and we should implement it as a fast-follow.

@1ec5 1ec5 linked a pull request Apr 9, 2020 that will close this issue
2 tasks
@1ec5
Copy link
Contributor

1ec5 commented Apr 9, 2020

Fixed in #418.

@1ec5 1ec5 closed this as completed Apr 9, 2020
@1ec5 1ec5 modified the milestones: v1.0.0, v0.31.0 (v0.40) Apr 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backwards incompatible changes that break backwards compatibility of public API op-ex Refactoring, Tech Debt or any other operational excellence work.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants