-
Notifications
You must be signed in to change notification settings - Fork 2
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
Fix unhelpful type error #55
Conversation
| /// This overload fixes compiler errors. | ||
| /// | ||
| /// To explicitly specify the type, use ``resolve(_:name:)`` or ``resolve(_:)`` instead. | ||
| /// | ||
| /// Without this overload, adding extraneous arguments to an `init` inside of the callback | ||
| /// passed to ``Service/init(_:_:name:_:)`` does not error at the `init` call, but at the top | ||
| /// of the ``Factory/register(builder:)`` block. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth adding a note mentioning that this overload doesn't appear in the documentation just incase someone is confused why it isn't.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that makes sense to me.
| @@ -19,7 +19,7 @@ public extension Resolver { | |||
| /// Get an unnamed dependency of the specified type. | |||
| /// - Returns: The unnamed instance of the type. | |||
| /// - Important: There must be an unnamed registration of the specified type. When resolving a | |||
| /// named dependency, use ``resolve(_:name:)`` or ``resolve(name:)`` instead. | |||
| /// named dependency, use ``resolve(_:name:)`` or ``Resolver/resolve(name:)`` instead. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did this link not work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It used to, but for some reason when I added the overload it didn't anymore, and I had to specify the base type.
b3fca36
to
8471573
Compare
Issue Link
Fixes #54
Jira issue: THOS-5
Overview of Changes
This PR fixes type resolution errors. Basically, what would happen in a situation like this:
is that Swift would be unable to figure out the generic type argument to
r.resolve()at [2], since the initializer forCdoesn't take ab. Because it can't determine the return type, swift would report "type of expression is ambiguous without more context", completely ignoring the fact that it's not meant to be there. Due to the result builder transform, the error would be reported at [1] instead of [2].Adding an overload that returns
Anyworks around this problem: if it can't figure out how to callresolve<T>() -> T, it tries the other overload,resolve() -> Any. This allows it to complete type-checking and realize thatbshouldn't even be there.Anything you want to highlight?
I marked the overload as
@available(*, unavailable). This has two consequences:var x: Any = Factory.shared.resolve(), this is inferred asresolve<Any>()instead of the new overload.var x = Factory.shared.resolve(), still emits an error:However, it has the downside that the new overload doesn't show up in the documentation. Using
deprecatedinstead ofunavailablemakes it show up in the documentation, but also makes point 1 no longer true, and downgrades point 2 from an error to a warning.Test Plan
Paste the code snippet above (or any equivalent) into a project that imports Dependiject. You should see the error at [2] instead of [1].