# Overloading operator ()

**URL:** <https://forums.swift.org/t/overloading-operator/14155>\
**Category:** Using Swift\
**Tags:** operators\
**Created:** [July 1, 2018, 7:06pm UTC](https://forums.swift.org/t/overloading-operator/14155 "2018-07-01T19:06:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![medismailben](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/medismailben/32/2992_2.png) [@medismailben](https://forums.swift.org/u/medismailben)\
**Post date:** [July 1, 2018, 7:06pm UTC](https://forums.swift.org/t/overloading-operator/14155/1 "2018-07-01T19:06:28Z")

</div>

Hi,

I'm working on a project where I'd like use the parentheses as method identifier.

C++ equivalent: `void operator()()`

I've tried overloading it but it looks like the compiler doesn't allow it.

Am I doing it wrong or is this feature not implemented yet ?

Thanks,  
Ismail

---

<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:** [July 1, 2018, 7:14pm UTC](https://forums.swift.org/t/overloading-operator/14155/2 "2018-07-01T19:14:47Z")

</div>

Can you elaborate a little bit more on what Swift code you wish to be able to write? `()` is not an operator in swift but an empty tuple aka `typealias Void = ()`

---

<div class="post-metadata">

**Author:** ![medismailben](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/medismailben/32/2992_2.png) [@medismailben](https://forums.swift.org/u/medismailben)\
**Post date:** [July 1, 2018, 7:29pm UTC](https://forums.swift.org/t/overloading-operator/14155/3 "2018-07-01T19:29:45Z")

</div>

Basically, I'd like to have a method in my class that use () as an identifier.  
I'd like to be able to call my instantiated object this way:

```
let obj : Object()
let s = obj("test")

```

---

<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:** [July 1, 2018, 7:31pm UTC](https://forums.swift.org/t/overloading-operator/14155/4 "2018-07-01T19:31:01Z")

</div>

Would a `subscript` serve your needs here?

```swift
class MyObject {
  subscript (_ value: String) -> SomeType { ... }
}

let object = MyObject()
let s = object["test"]

```

I don't think Swift will ever support parentheses in that way. It's kind of strange and it reads ambiguous.

---

<div class="post-metadata">

**Author:** ![medismailben](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/medismailben/32/2992_2.png) [@medismailben](https://forums.swift.org/u/medismailben)\
**Post date:** [July 1, 2018, 7:35pm UTC](https://forums.swift.org/t/overloading-operator/14155/5 "2018-07-01T19:35:33Z")

</div>

Yeah in my case I'd do the job perfectly, but what if my class is also a Collection.

It would be great to have the () option.

---

<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:** [July 1, 2018, 7:39pm UTC](https://forums.swift.org/t/overloading-operator/14155/6 "2018-07-01T19:39:35Z")

</div>

Your type can have as much subscripts as you'd like, the only limitation that you can reach is that some subscripts can become ambiguous at call-site but you can explicitly tell the compiler the types to resolve such collisions. In other situation you can add a _view type_ to your class that will provide the functionality you need.

```swift
class MyObject {
  struct DataView {
    let object: MyObject
    subscript (_ value: String) -> Data { ... }
  }
  var data: DataView { return DataView(object: self) }
}

let object = MyObject()
let s = object.data["test"]

```

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [July 1, 2018, 8:11pm UTC](https://forums.swift.org/t/overloading-operator/14155/7 "2018-07-01T20:11:05Z")

</div>

> [@DevAndArtist](#):
>
> I don't think Swift will ever support parentheses in that way. It's kind of strange and it reads ambiguous.

We literally just accepted [SE–0216 “Introduce user-defined dynamically "callable" types”](https://github.com/apple/swift-evolution/blob/master/proposals/0216-dynamic-callable.md) two days ago. It would be wildly incongruous to let people define wholly dynamic “callable” behavior, but to somehow decide that \*statically\* callable types would not fit in the language.

Of course statically-callable types make sense, of course they fit in the language, and of course square-brackets are a poor substitute for round ones in this scenario. After all, if square brackets were good enough for “callable” types, then we would’ve demanded that SE–0216 use square brackets.

But we didn’t.

Because square brackets are the wrong symbol for “callable” types.

The right symbol is a pair of parentheses, just like we use when calling any other function or method. When a user-defined type represents a function, it deserves and ought to be able to use parentheses when it is “called”.

For example:

```swift
class DifferentiableFunction<T: FloatingPoint> {
  var f: (T) -> T
  var df: DifferentiableFunction?
  init(f: @escaping (T) -> T) { self.f = f }

  // This should be “callable” with parentheses not square brackets:
  subscript(x: T) -> T { return f(x) }
}

```

---

<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:** [July 1, 2018, 8:20pm UTC](https://forums.swift.org/t/overloading-operator/14155/8 "2018-07-01T20:20:35Z")

</div>

Thx for mentioning it Nevin, when I replied I only had the dynamic lookup attribute in mind and to be honest I never looked that closely into that last proposal.

Btw. did you forget the attributes on your example type?

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [July 2, 2018, 12:25am UTC](https://forums.swift.org/t/overloading-operator/14155/9 "2018-07-02T00:25:26Z")

</div>

That is the "functor", or function object, concept in C++. We haven't had that concept in Swift. The `subscript` suggestion from @DevAndArtist doesn't really match because a subscript access implies more of a sub-object, while a functor is more of a mapping (like conventional functions and closures).

But, as @Nevin said, we have taken steps that may lead to a Swift functor concept someday.

---

<div class="post-metadata">

**Author:** ![manuel.pagliai](https://avatars.discourse-cdn.com/v4/letter/m/9de0a6/32.png) [@manuel.pagliai](https://forums.swift.org/u/manuel.pagliai)\
**Post date:** [December 4, 2022, 11:53am UTC](https://forums.swift.org/t/overloading-operator/14155/10 "2022-12-04T11:53:34Z")

</div>

You can use  
`public func callAsFunction() { /* your code */ }`

[https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID622](https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID622)

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [December 4, 2022, 11:13pm UTC](https://forums.swift.org/t/overloading-operator/14155/11 "2022-12-04T23:13:59Z")

</div>

This terser, anonymous function syntax, proposed by @davdroman

```swift
struct Foo () {
    func (...)
}

```

feels much better than these verbose, supported alternatives:

```swift
struct Foo () {
   func callAsFunction (...)
}

// Or
@dynamicCallable
struct Foo () {
   func dynamicallyCall (withArguments: ...)
}

```

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [December 4, 2022, 11:28pm UTC](https://forums.swift.org/t/overloading-operator/14155/12 "2022-12-04T23:28:29Z")

</div>

This was all discussed in the proposals for these features. That doesn’t mean it’ll never change, but a four-year-old “Using Swift” thread isn’t the place to relitigate it.
