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

**URL:** <https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605>\
**Category:** Announcements\
**Created:** [May 16, 2019, 11:09pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605 "2019-05-16T23:09:07Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![glessard](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glessard/32/409_2.png) [@glessard](https://forums.swift.org/u/glessard)\
**Post date:** [May 18, 2019, 12:49am UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/61 "2019-05-18T00:49:36Z")

</div>

`callSelf` is really not bad. I like it.

---

<div class="post-metadata">

**Author:** ![Les\_Pruszynski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/les_pruszynski/32/797_2.png) [@Les\_Pruszynski](https://forums.swift.org/u/Les_Pruszynski)\
**Post date:** [May 18, 2019, 3:24am UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/62 "2019-05-18T03:24:56Z")

</div>

> [@xwu](#):
>
> or even `selfCall` ?

I like that very much.

---

<div class="post-metadata">

**Author:** ![Jean-Daniel](https://avatars.discourse-cdn.com/v4/letter/j/977dab/32.png) [@Jean-Daniel](https://forums.swift.org/u/Jean-Daniel)\
**Post date:** [May 18, 2019, 5:38am UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/63 "2019-05-18T05:38:56Z")

</div>

> [@riley](#):
>
> Probably mentioned before, but what about having _global_ functions of the form `call(_:)` whose first argument is the type to call, and that can be quasi-overloaded with various types and parameters?

This ship has sailed. The proposal is accepted. The only change we can discuss is the function name.

---

<div class="post-metadata">

**Author:** ![nh7a](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nh7a/32/1141_2.png) [@nh7a](https://forums.swift.org/u/nh7a)\
**Post date:** [May 18, 2019, 3:16pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/64 "2019-05-18T15:16:17Z")

</div>

```swift
func callSelf() {}

```

And I think it's better than `callInstance()` or `callValue()` (which I liked first) because `"Self"` grabs an attention and makes it feel something special.

---

<div class="post-metadata">

**Author:** ![davedelong](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davedelong/32/38708_2.png) [@davedelong](https://forums.swift.org/u/davedelong)\
**Post date:** [May 18, 2019, 4:31pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/65 "2019-05-18T16:31:07Z")

</div>

If this is really going to be a function (which [I very much disagree should happen](https://forums.swift.org/t/se-0253-callable-values-of-user-defined-nominal-types/24177/19), but oh well), then IMO there's really only a single of logical name for what it should be:

```swift
func _ (arg1: Type1)

```

This is not a feature like `subscript`, because we've decided we're still going to have `func` as part of the signature, which is not the case when declaring a `subscript`-able type. Therefore, I don't think we need an additional special keyword-name.

What we're technically describing here is the ability to call a function on a type, but where that function _does not have a name_. In Swift, the syntax for "this is unnamed" is to use `_`. So having the callable function be `func _ (...)` is consistent with existing syntax, because it means "This is a function, but it has no name", which implies you'd call it by omitting the name, which means it is the "callable" function:

```swift
struct Adder {
    let base: Int
    func _ (adding value: Int) -> Int { return base + value }
}

let a = Adder(base: 40)
let answer = a(adding: 2) // calling a function with an omitted name

```

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [May 18, 2019, 4:44pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/66 "2019-05-18T16:44:04Z")

</div>

I'd rather see a generalization of subscripts and functions where both can be unnamed, and where both can be declared globally, not just on types.

```swift
func (value: Int) -> Int { return value }
// The only inconsistency here is the set of rules for parameter labels.
subscript (value value: Int) -> Int { return value }

// usage
ModuleName(value: 42)
ModuleName[value: 42]

```

* * *

Syntactically wise the accepted form of the proposal is very very _special_ and it just adds a lot mental load than a generalization of function names with precedence to get-only subscripts. In the current state I'd rather reject the whole proposal. (That is just my personal opinion.)

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [May 18, 2019, 10:05pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/67 "2019-05-18T22:05:13Z")

</div>

I don't care to bikeshed much but `callFunction` feels very strange. I'd prefer we use `call`, which makes more sense and reads better, and we could all just accept that some folks may define `call` methods that collide with this functionality, even if they don't use this functionality.

---

<div class="post-metadata">

**Author:** ![lancep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancep/32/1509_2.png) [@lancep](https://forums.swift.org/u/lancep)\
**Post date:** [May 19, 2019, 9:11pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/68 "2019-05-19T21:11:26Z")

</div>

I very much like this approach, mostly because it prevents people from doing `instance.callFunction(foo, bar: baz)`

---

<div class="post-metadata">

**Author:** ![hartbit](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hartbit/32/738_2.png) [@hartbit](https://forums.swift.org/u/hartbit)\
**Post date:** [May 20, 2019, 7:14am UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/69 "2019-05-20T07:14:44Z")

</div>

I don't see what's the problem with people directly calling the function?

---

<div class="post-metadata">

**Author:** ![sjavora](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sjavora/32/9323_2.png) [@sjavora](https://forums.swift.org/u/sjavora)\
**Post date:** [May 20, 2019, 10:00am UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/70 "2019-05-20T10:00:16Z")

</div>

`callOnSelf`?

---

<div class="post-metadata">

**Author:** ![dabrahams](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dabrahams/32/9815_2.png) [@dabrahams](https://forums.swift.org/u/dabrahams)\
**Post date:** [May 20, 2019, 10:06am UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/71 "2019-05-20T10:06:43Z")

</div>

I'm curious as to what value you see "perform" adding in this context?

still-likin'-functionCall-ly y'rs,

Dave

---

<div class="post-metadata">

**Author:** ![Antipodean](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/antipodean/32/7881_2.png) [@Antipodean](https://forums.swift.org/u/Antipodean)\
**Post date:** [May 20, 2019, 10:49am UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/72 "2019-05-20T10:49:39Z")

</div>

Dear Swift,

I wonder whether, since these special functions will be able to be called directly on an instance/value, without referring explicitly to a member function via dot syntax, this could be encoded in the name:

```swift
func implicitCall() { ... }

```

or

```swift
func callImplicitly() { ... }

```

It occurs to me that, at heart, the distinguishing feature of these functions is that they are enabled to be called implicitly. This is their principal (and perhaps only) point of difference to all other member functions, which must be called explicitly by name, using dot notation.

It is merely a thought which I hope is of some use or interest to you.

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [May 20, 2019, 1:39pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/74 "2019-05-20T13:39:10Z")

</div>

> [@dabrahams](#):
>
> I'm curious as to what value you see "perform" adding in this context?

`functionCall` reads like a noun to me, along the lines of `NSInvocation`. That makes it feel extremely awkward to me and doesn’t feel like it fits with the API Design Guidelines. Even if an argument can be constructed that it does align with the guidelines somehow I don’t think I will be alone in reading it differently.

`callFunction` is even worse. Consider this:

```swift
struct Foo<T> {
    func callFunction(_ comparator: (T) -> Bool) -> Int { ... }
}
let foo = Foo<Int>()
foo.callFunction { $0 < 42 }

```

In this example, the call site reads as if _the only thing it does_ is to call the function that is passed in. This is unlikely to be the case and would hardly make sense if it was (why wouldn’t the caller just invoke it directly). I think including the verb “perform” by explicitly stating `performFunctionCall` (or simply `performCall`) is much more descriptive and far more clear at the point of use.

Personally, I think the core team is making a mistake in forcing a name on what is really an anonymous method. I especially dislike the idea that “magic” member names _automatically_ have special behavior without anything (protocol, attribute, etc) that “activates” the special behavior. I think this direction is a bad idea and I’m disappointed to see that the core team is inviting proposals to drop attributes that are currently used to enable “magic” members.

If you’re going insist on a named method, please prioritize clarity at the point of **explicit** use for the name even if explicit use is not intended. Which of these call sites do you think reads more clearly?

```swift
foo.callFunction { $0 < 42 }
foo.functionCall { $0 < 42 }
foo.performCall { $0 < 42 }
foo.performFunctionCall { $0 < 42 }

```

In the latter two, I think it is substantially more clear that `foo` is “performing” the call and playing the role of a function that is called. Personally, I like `performCall` the best out of the options the core team appears to be willing to consider. I think a plain `call` is also fine, but the core team has already ruled that out.

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [May 20, 2019, 2:09pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/75 "2019-05-20T14:09:08Z")

</div>

> [@anandabits](#):
>
> Personally, I think the core team is making a mistake in forcing a name on what is really an anonymous method

I agree with this. The only benefit I see to having an identifier is to allow partial application. For this, I feel it would be better if `call` became a keyword, in the same vein as `init`. However, the Core Team already vetoed this idea, so all we can do is hope to minimize the damage.

---

<div class="post-metadata">

**Author:** ![michelf](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michelf/32/1180_2.png) [@michelf](https://forums.swift.org/u/michelf)\
**Post date:** [May 20, 2019, 3:03pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/76 "2019-05-20T15:03:48Z")

</div>

I don't agree there is no benefit in having it be a regular function: it is less disruptive if there is no change in grammar and is easier to adopt for the compiler and the rest of the ecosystem.

I agree though that being able to refer by name to the function pretending to be anonymous makes things awkward. I don't think there's a way around this if it remains a regular function. I too favor something were the function does not have a real name, but here we are.

* * *

Since we're bikeshedding, anybody thought of this one yet?

```swift
foo.actUpon { $0 < 42 }
foo.actUpon("this")

```

I find this one both fitting and hilarious because it sounds like a precise word about doing something vague.

* * *

There's something peculiar about trying to follow the naming guidelines here. Normally, the name of the function should go hand in hand with the parameter names, forming something grammatically okayish when put together. But in our case, the name of the function is expected to be disassociated from the name of the arguments when in use...

Take the example above, I called the function `actUpon` even though the correct name should have been `act(upon:)`. But if you use it as a callable value you end up with `foo(upon:)` which is just silly.

So I don't think it makes sense to follow too strictly the guidelines about function naming here. We need new guidelines about naming parameters for those callable values as the current rules aren't cut for this. And perhaps the name of the "anonymous" function could be chosen so to fit parameters following the guidelines in a way that the result is grammatically okayish too.

* * *

First round: which one reads better?

```swift
foo(source: "hello.txt", destination: "world.txt")

func actUpon(source: String, destination: String)
func callFunction(source: String, destination: String)
func functionCall(source: String, destination: String)
func performCall(source: String, destination: String)
func performFunctionCall(source: String, destination: String)
func callSelf(source: String, destination: String)

```

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [May 20, 2019, 3:08pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/77 "2019-05-20T15:08:07Z")

</div>

> [@michelf](#):
>
> I don't agree there is no benefit in having it be a regular function: it is less disruptive if there is no change in grammar and is easier to adopt for the compiler and the rest of the ecosystem.

It would be the exact same grammar as `init`. And there are a handful of compiler developers, and 100s of thousands, if not millions, of Swift language users. To optimize for ease of compiler writing seems like the wrong trade-off. Anyway, this an agree-to-disagree moment, because the CT has spoken, and this is not the place to try and relitigate.

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [May 20, 2019, 3:10pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/78 "2019-05-20T15:10:23Z")

</div>

> [@michelf](#):
>
> There's something peculiar about trying to follow the naming guidelines here

It was pointed out during review that there's no way to have a nameable function that will always read well when invoked by name, as well as by variable. (i.e. both `var.callFunction(...)` and `var(...)`)

---

<div class="post-metadata">

**Author:** ![shpakovski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/shpakovski/32/5559_2.png) [@shpakovski](https://forums.swift.org/u/shpakovski)\
**Post date:** [May 20, 2019, 4:35pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/79 "2019-05-20T16:35:23Z")

</div>

> [@Avi](#):
>
> It would be the exact same grammar as `init`.

I love this idea, no `func callSelf(…) {}` or the like, just `call(…) {}`, right?

---

<div class="post-metadata">

**Author:** ![lancep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancep/32/1509_2.png) [@lancep](https://forums.swift.org/u/lancep)\
**Post date:** [May 20, 2019, 4:38pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/80 "2019-05-20T16:38:04Z")

</div>

Why have two ways to do something when one will suffice?

---

<div class="post-metadata">

**Author:** ![lancep](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lancep/32/1509_2.png) [@lancep](https://forums.swift.org/u/lancep)\
**Post date:** [May 20, 2019, 5:51pm UTC](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605/81 "2019-05-20T17:51:43Z")

</div>

Thinking about it more, one disadvantage to having no name for this would be the inability to make a key path to this function.

[Previous page](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605.md?page=3)

[Next page](https://forums.swift.org/t/accepted-with-modification-se-0253-callable-values-of-user-defined-nominal-types/24605.md?page=5)
