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

[SR-11017] dynamicallyCall can't be mutating #53407

Open
mattneub opened this issue Jun 25, 2019 · 3 comments
Open

[SR-11017] dynamicallyCall can't be mutating #53407

mattneub opened this issue Jun 25, 2019 · 3 comments
Labels
compiler The Swift compiler in itself improvement

Comments

@mattneub
Copy link

Previous ID SR-11017
Radar None
Original Reporter @mattneub
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee None
Priority Medium

md5: 0d961a467f1fb47f1346ba53df523c0f

Issue Description:

@dynamicCallable struct S {
    func dynamicallyCall(withKeywordArguments kvs:KeyValuePairs<String, String>) {}
}
var s = S()
s(testing:"testing")

That compiles. But now suppose our dynamically called method is supposed to mutate the struct. If we put `mutating` in front of `func`, we get a compile error: "cannot call value of non-function type 'S'".

I can work around this by making this a class instead of a struct. For example, this works:

@dynamicCallable class Flock {
    var d = ["partridge":"in a pear tree"]()
    func dynamicallyCall(withArguments: [String]) {}
    func dynamicallyCall(withKeywordArguments kvs:KeyValuePairs<String, String>) {
        if kvs.count == 1 {
            let (key,val) = kvs.first!
            switch key {
            case "remove": d[val] = nil
            default: break
            }
        }
    }
}
var flock = Flock()
flock(remove:"partridge")

But if Flock is a struct, I can't find any way to do that.

@theblixguy
Copy link
Collaborator

cc @dan-zheng

@dan-zheng
Copy link
Collaborator

Supporting for mutating func dynamicallyCall should be possible.
Similar support for mutating func callAsFunction is being implemented in #24299
There are still some bugs (namely with mutating func callAsFunction + IUO), but once done, the approach can be ported to dynamicallyCall.
I'm not sure whether this change to dynamicallyCall warrants any Swift Evolution discussion - I imagine it's noncontroversial.

@mattneub
Copy link
Author

Surely a struct method that cannot be marked `mutating` and called on a `var` reference to that struct is not Swift.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement
Projects
None yet
Development

No branches or pull requests

3 participants