-
Notifications
You must be signed in to change notification settings - Fork 164
Support @autoclosure within arguments at @DependencyEndpoint
#160
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
Conversation
| $1.isInout | ||
| ? "&p\($0)" | ||
| : $1.isAutoclosure | ||
| ? "p\($0)()" | ||
| : "p\($0)" |
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.
#159 style seemed so cool that I had to try it myself. Originally, I was writing code like this 😅
let appliedParameters =
parameters
.enumerated()
.map {
if let typed = $1.type.as(AttributedTypeSyntax.self),
typed.specifier?.tokenKind == .keyword(.inout) {
return "&p\($0)"
}
if let typed = $1.type.as(AttributedTypeSyntax.self),
typed.attributes.contains(where: {
$0.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.name.tokenKind == .identifier("autoclosure")
}) {
return "p\($0)()"
}
return "p\($0)"
}
.joined(separator: ", ")| .attributeName | ||
| .as(IdentifierTypeSyntax.self)? | ||
| .name | ||
| .tokenKind == .identifier("autoclosure") |
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.
#159 uses text but I thought tokenKind might be a bit better. Since I'm new to SwiftSyntax, I'll respect your choice.
| } | ||
|
|
||
| extension TupleTypeElementSyntax { | ||
| fileprivate var isAutoclosure: Bool { |
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.
| """ | ||
| struct Foo { | ||
| @DependencyEndpoint | ||
| var bar: (_ a: @autoclosure () -> Int, _ b: () -> Int, _ c: @autoclosure () -> Int) -> Void |
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.
| $1.isInout | ||
| ? "&p\($0)" | ||
| : $1.isAutoclosure | ||
| ? "p\($0)()" |
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.
Only () is OK because @autoclosure can be used only when there are no parameters.
Also inout expression cannot be used on @autoclosure so we don't need to consider the situation when $1.isInout && $1.isAutoclosure.
mbrandonw
left a comment
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.
Thanks for taking the time to PR this!
mbrandonw
left a comment
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.
Great, thanks @xiii111!
|
@mbrandonw Thank you! |

Resolve #155
Cause
In general, the above code can typically be transformed into the following code without any issue. This is because the type of argument
apassed tofunc barmatches the type of the argumentapassed tovar bar.This also applies to simple closures.
However, when considering closures marked with
@autoclosure, the type of the argumentapassed tofunc barmight not match the type of the argumentawhen passing tovar bar.Solution
Simply adding
()should resolve this bug.Concern
I might be overly concerned, but I feel there's a slight difference between directly invoking
var barand indirectly throughfunc bar. When directly callingvar bar, it receives() -> String. However, when callingvar barindirectly throughfunc bar, it receives() -> (() -> String)(). I don't expect this difference to cause any change in behavior, but if there might be an issue, I'd appreciate some advice!