@rxwei makes an interesting suggestion, although I do believe Swift naming guidelines elide 'with' for parameter labels.
I'm on the fence about whether the function itself should be a verb ("dynamically call") or simply a noun ("dynamic call"); if we're going to call the attribute @dynamicCallable and not @dynamicallyCallable, I think on balance I'd favor the original.
Swift naming guidelines elide âwithâ for parameter labels.
@xwu I don't think it exists in the API design guidelines, but I'd appreciate it if you can point me to such writing.
Dynamic calls can be mutating the underlying object. I believe a verb-like name is more appropriate. For example:
let array: PyValue = [1, 2, 3]
array.append(4)
If we desugar this dynamic call, the dynamicallyCall name sounds very reasonable. It's an action (call "append"), and I can't tell whether it's mutating the underlying data or not.
While a noun-like name below seems to imply that the function is "returning a dynamic call named 'append' with argument 4", without performing the call action.
You can see it in the detailed write-up of how the guidelines are applied in SE-0005 and SE-0006. For example:
In migrating the standard library from Swift 2 to Swift 3 based on the naming guidelines, joinWithSeparator(_:) becomes joined(separator:).
In translating Objective-C names, fillWithBlendMode(_:alpha:) becomes fill(blendMode:alpha:) (and many other examples).
It's impossible to say whether a dynamic call is more likely to be mutating or not, nor do I think it's wise to associate that consideration with the name of the underlying method here. At best, the name could be ambiguous, but ultimately I don't think that the consideration is applicable since a dynamic call self-evidently rules out any way to know for sure.
I think withArguments here conforms with the guidelines. The obj -> swift grand renaming is not part of the guidelines as far as I know. At least I do not see evidence of this in the guidelines.
[Full bikeshed mode enabled]
Shouldn't it be called withParameters since we are defining the the kind of parameters that the function we are calling can take? I guess it is both parameters (the key) and arguments (the value to the key).
I do think that named could be dropped as Method describes the name.
IMO, withArguments conforms to the official guidelines. The guidelines also have an example using with:.
I don't have a strong opinion about named:. On one hand, I agree that dropping would make it simpler. OTOH, given that users will implement this method for syntactic sugar someCallableStuff(), there's no problem with this method being verbose for clarity.
I think "parameter" refers to a formal parameter in the function declaration. "Argument" refers to a value being passed in to match a parameter's type.
Every function has a function name, which describes the task that the function performs. To use a function, you âcallâ that function with its name and pass it input values (known as arguments) that match the types of the functionâs parameters. A functionâs arguments must always be provided in the same order as the functionâs parameter list.
It falls under the guideline of âomit useless wordsââthe Objective-C to Swift renaming was conducted to adopt Swift API naming guidelines and demonstrates how those guidelines are intended to be applied. See SE-0005/6 for more.
Yes, âwith:â conforms to the guidelines because, where a label is necessary, it cannot be omitted as then there would be no label. However, where there are other words in the label, âwithâ is elided, as the parentheses surrounding the arguments imply âwith.â See SE-0005/6.
Cool. We are good then because withArguments is referring to the arguments of the dynamic call. This is much different than saying withString or something along those lines. I am not saying with should stay but we do need a label here.
I think a better name for this label should be passing. Or passingArguments or passingKeywordArguments
You call the greet(person:) function by passing it a String value after the person argument label, such as greet(person: "Anna"). -Swift Book (Emphasis mine)
I prefer just passing but being more descriptive is not too bad.
OTOH, I do agree passing adds clarity for dynamicallyCall. dynamicallyCall(arguments:) reads "dynamically call these arguments", which would be the wrong meaning (although in cases it is expected to convey that meaning it would be called dynamicallyCallArguments(_:)).
In the normal case it would be true. My hesitation is based on the nested, "meta" aspect of the method; dynamicallyCall, dynamicallyCallMethod are just forwarding their call to the target language and while they do take arguments themselves to make this happen, they are just "forwarding" or passing the call down.
Although @dynamicMemberLookup and @dynamicCallable are mainly designed for interoperability, but these features alone do not have any indication of meta-programming or language interop. They are just pure syntactic sugar.
At least c# seems to blend these distinctions when referencing to syntatic sugar like variadic parameters.
If you look at this from a different angle, isn't the dynamicCall methods just syntactic sugar for swift currently limited variadic parameters? I am more convinced the label should be called parameters instead of arguments
The left hand side of a function type is called "parameter types".
At a function call site, the stuff being passed are called "arguments".
This is also followed in the implementation detail of Swift. Concrete things being passed into a function are consistently referred to as "arguments", e.g. ApplyExpr arguments, ApplyInst arguments, SIL basic block arguments.