[Accepted with Modification] SE-0253 - Callable values of user-defined nominal types

Please be specific: what is the trip-up you anticipate that would be worse than what happens when a programmer encounters an unfamiliar

func _(…)

?

1 Like

Please also consider the number of likes given to each proposal.

We'll consider everything we can, but as I've said before, I encourage you to say what you want to say instead of hoping that we'll pick up on it from cross-tabulating likes.

My vote is on:

func _ (arg1: Type1)

I found the arguments of @davedelong very convincing

2 Likes

IMHO both func function and func callSelf are preferable to func functionCall

functionCall isn't bad. Two other suggestions:

  • callWith(...). Read with the parameter list, the "with" applies to all of the parameters.
  • dynamicallyCall(...), as a sort of soft replacement of (part of) the existing feature. We would either deprecate dynamicallyCall(withArguments:) and remove it at our next source break, or use it only if @dynamicCallable is applied to the type. This would take advantage of users' familiarity with the existing feature.

… oh no… now I'm doing it.

for your consideration: anonymousMethod

The suggestion to borrow Python's *args/**kwargs features is very interesting (even outside of callable members), but I think we should tackle these one at a time. Ideally we would've had that feature first and introduced callable stuff later, but we can still unify it all in a source-compatible way later on.

The trouble that seems most likely is that somebody sees func functionCall and thinks “hmm, that’s a really strange name for a method. I’m going to clean that up”. They search for functionCall() and don’t find any references so they either delete it or they rename it. Now the project doesn’t build, and the error message probably isn’t very helpful.

If the method name is of a more traditional verb form func performCall, func callSelf or @beccadax’s new addition of callWith (which I like more than most of the alternatives), then I think the instinct to “clean it up” will be less so I think the verb form is better. That said, I some people might still scratch their head a little. If there is a more conventional name in the domain such as combine even the verb form might inspire the instinct to “clean up”.

If instead they saw something unusual about the declaration besides the grammar of the method name, such as @callable, a custom declaration type, or an unnamed method they will probably not have the instinct to “clean it up”. Instead they will have the instinct to search and learn about the language feature.

In general, the path of “magic” member names that on their own are sufficient to invoke syntactic sugar and other compiler magic seems inadvisable to me. Since the core team seems committed to this approach, I really hope you will at least consider going with a verb form for this feature. How do you feel about callWith?

11 Likes

This appears to me to contradict your point. Apparently people can see the name is strange, so good. Also, if I just edit some random thing I don't understand and then the project fails to build, I do two things:

  1. Inspect the failure sites.
    Oh, it's complaining that instance(...signature of method I just renamed...) isn't valid.
    Hmm... this looks like a function call. Nope, must be a coincidence.

  2. Revert the thing I just changed.
    Oh, it's building again.
    Guess it wasn't a coincidence.

We can never make things entirely idiot-proof. Throwing a bunch of attributes and loud, scary syntax around is totally not worth it IMO. I don't think it really matters a great deal what we call it.

1 Like

I like callWith.

1 Like

I like callWith as well, if we are going with a magic name.

I do find the magic name approach to be pretty surprising direction for Swift, given how init and subscript are clearly set apart from a regular function call. I haven’t spent a lot of time with this though.

6 Likes

+1

I’ve read all of the suggestions and I think func callFunction it’s the best.

I like its because it has the word function spelled out. In addition the letter f is capitalized which makes it pop, gives me the false illusion of being some type and thus extra important. What kind of call is this? It is a Function call. Can’t miss that big F. How do I know this is a function call? Just read the first two parts. func call...

I swore I wouldn't chime in on this thread because I think callFunction is fine and I don't have a strong opinion about other options given the imposed constraints. I also don't know if I'm completely serious about this idea, but just to throw something out into the mix that I don't think has been suggested yet (so maybe it's terrible):

What if the name of the special function was the keyword func?

struct CallableThing {
  func func(arg: Int) -> Int { ... }
}

Pros

  • It's not valid syntax today, so the declaration can't collide with existing source. (You can write func `func` to get a function named func, which is probably uncommon.)
  • Users can write Foo.func to get a function reference (actually possible today with func `func` ), which has some nice symmetry with Foo.init.

Cons

  • Doesn't contain "call", which seems to have importance to the core team in terms of expressing its intent.
  • Looks redundant/a bit silly.
  • We'd need to answer the question: would func func be identical to func `func` ?
2 Likes

I didn’t see this mentioned in the proposal in github so I don’t know if this was covered, but does this ability also apply to a function property?

struct Thing {
    let callFunction: (String) -> Int
}

If so, then I’m not sure if I like callFunction or anything that’s overly verbose, but regardless I think this scenario should be considered too as I haven’t seen it mentioned at all if it will be possible or not.

+1. It reads better for me than all the proposed alternatives.

I find "callFunction" and "callAsFunction" to be pretty good. We can go even further with that and use "asFunction". It may sound weird, but in my opinion it has some advantages:

  1. It describes what this feature does accurately. After all, we use instances of types as functions.
  2. The function itself can be referenced with "callable.asFunction" which has a nice read to it.
  3. Even though it is a valid identifier, it's not going to be a widely used method name.
7 Likes

They stop, think, look it up, learn a bit more on it, move on? Sometimes the right friction and googlability helps. The vote is on the anonymous syntax btw ;).

2 Likes

Big fan of @davedelong's func _ suggestion.

3 Likes

_ doesn't seem very Googleable. I know this is not a vote, but I like performCall.