Skip to content

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

Open
@mattneub

Description

@mattneub
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions