# Operator member syntax

**URL:** <https://forums.swift.org/t/operator-member-syntax/31773>\
**Category:** Discussion\
**Created:** [December 17, 2019, 7:15pm UTC](https://forums.swift.org/t/operator-member-syntax/31773 "2019-12-17T19:15:17Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [December 17, 2019, 7:15pm UTC](https://forums.swift.org/t/operator-member-syntax/31773/1 "2019-12-17T19:15:17Z")

</div>

Hi folks,

Currently, the only way to directly reference an overloaded operator is by providing a contextual type, which may be verbose:

```swift
// Explicit contextual type is verbose.
_ = (+) as (Float, Float) -> Float
_ = (+) as (Float) -> Float

// No verbosity if type can be inferred, but inference isn't always possible.
[1, 2, 3].reduce(0, +)

```

How do folks feel about supporting operator member syntax, just like references to other static members? It's currently not supported, but I wonder if it's just a parser deficiency:

```swift
_ = Float.+
_ = Float.(+)
_ = Float.`+`

```

```swift
test.swift:1:10: error: use of unresolved operator '.+'
_ = Float.+
         ^~
test.swift:2:11: error: expected member name following '.'
_ = Float.(+)
          ^
test.swift:3:11: error: expected member name following '.'
_ = Float.`+`
          ^

```

Requiring backticks around the operator name may be nice to prevent confusion for operators that begin with a leading `.`, like [`SIMD.(.<)`](https://developer.apple.com/documentation/swift/simd/3139460):

```swift
_ = Float.`+`
_ = SIMD2<Float>.`.<`
_ = SIMD2<Float>.`.<(_:_:)`

```

* * *

Sorry if this topic has been discussed previously.

I did a search and found results for module-qualified names, which seems orthogonal and doesn't address this kind of ambiguity (e.g. the `Swift` module defines multiple `+` operators):

- [Pitch: Fully qualified name syntax](https://forums.swift.org/t/pitch-fully-qualified-name-syntax/28482)
- [What the eff is DeclNameRef?](https://forums.swift.org/t/what-the-eff-is-declnameref/31594)
- [SR-8278: Syntax to disambiguate operators](https://bugs.swift.org/browse/SR-8278) (module qualification)

---

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [December 17, 2019, 7:18pm UTC](https://forums.swift.org/t/operator-member-syntax/31773/2 "2019-12-17T19:18:40Z")

</div>

Context: operator member syntax popped up in [a discussion](https://github.com/apple/swift/pull/27545/files#r358622964) regarding the `@derivative(of:)` and `@transpose(of:)` attributes for [differentiable programming](https://forums.swift.org/t/differentiable-programming-mega-proposal/28547).

These attributes reference a declaration, which may be an operator:

```swift
@derivative(of: Float.+)
func addDerivative(lhs: Float, rhs: Float) -> (value: Float, differential: (Float, Float) -> Float) { … }

```

In practice, qualified operator member syntax isn't necessary for these attributes. Type-checking for `@derivative(of:)` and `@transpose(of:)` currently require the operator to be defined in the same type context, where lookup is not ambiguous:

```swift
extension Float {
    @derivative(of: +(_:_:))
    static func addDerivative(lhs: Float, rhs: Float) -> (value: Float, differential: (Float, Float) -> Float) {
        (lhs + rhs, { $0 + $1 })
    }
}

```

But this motivated the discussion nonetheless!

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [December 17, 2019, 7:22pm UTC](https://forums.swift.org/t/operator-member-syntax/31773/3 "2019-12-17T19:22:01Z")

</div>

It doesn’t seem to extend very far. If there are multiple overloads; like `SIMD`, prefix/suffix variants; then it’s already back to being ambiguous.

There are also old operators that are declared as global functions, which would dodge this treatment.

---

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [December 17, 2019, 7:34pm UTC](https://forums.swift.org/t/operator-member-syntax/31773/4 "2019-12-17T19:34:58Z")

</div>

> [@Lantua](#):
>
> It doesn’t seem to extend very far. If there are multiple overloads; like `SIMD` , prefix/suffix variants; then it’s already back to being ambiguous.
> 
> There are also old operators that are declared as global functions, which would dodge this treatment.

It's fair that ambiguity is sometimes unavoidable.

I do think that operator member syntax enables an easy way to refer to unambiguous member operators. This is a big convenience for (common?) cases, like `Float.+(_:_:)` instead of `(+) as (Float, Float) -> Float)`.

---

<div class="post-metadata">

**Author:** ![t-ae](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/t-ae/32/3755_2.png) [@t-ae](https://forums.swift.org/u/t-ae)\
**Post date:** [December 18, 2019, 12:54am UTC](https://forums.swift.org/t/operator-member-syntax/31773/5 "2019-12-18T00:54:22Z")

</div>

What it'll be if the arguments' types are different, like `+(_: Date, _: TimeInterval) -> Date` or `+(_: Tensor<Float>, _: Float) -> Tensor<Float>`?

> [@dan-zheng](#):
>
> Requiring backticks around the operator name may be nice to prevent confusion for operators that begin with a leading `.` , like [`SIMD.(.<)`](https://developer.apple.com/documentation/swift/simd/3139460):

It looks `SIMD.(.<)` is this case.  
(Or what you intended to link is this?: [.\<(\_:\_:) | Apple Developer Documentation](https://developer.apple.com/documentation/swift/simd/3139462))

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [December 18, 2019, 1:13am UTC](https://forums.swift.org/t/operator-member-syntax/31773/6 "2019-12-18T01:13:38Z")

</div>

+1. This seems like something we should "obviously" allow.

I'm also in favour of removing the magic surrounding operators more broadly (e.g. I also think they should be allowed as instance members rather than only as static members - like `callAsFunction` [operator `()`] and `dynamicMemberLookup` [operator `.`] may be).

Otherwise, edge-cases like yours emerge where you'd like to treat an operator simply as a function with a weird name, and it becomes frustratingly difficult to do so. I've hit these kind of issues in the past as well.

---

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [December 18, 2019, 1:13am UTC](https://forums.swift.org/t/operator-member-syntax/31773/7 "2019-12-18T01:13:46Z")

</div>

> [@t-ae](#):
>
> What it'll be if the arguments' types are different, like `+(_: Date, _: TimeInterval) -> Date` or `+(_: Tensor<Float>, _: Float) -> Tensor<Float>` ?

If a member operator is overloaded for the same parameter arity and parameter labels, then direct references to it with no contextual type would be truly ambiguous.

However, this is no different than overloaded non-operator members:

```swift
struct S {
  func foo(_ x: Int) -> Int { x }
  func foo(_ x: Float) -> Float { x }
}

_ = S.foo

```

```swift
$ swift overload.swift
overload.swift:6:5: error: ambiguous use of 'foo'
_ = S.foo
    ^
overload.swift:2:8: note: found this candidate
  func foo(_ x: Int) -> Int { x }
       ^
overload.swift:3:8: note: found this candidate
  func foo(_ x: Float) -> Float { x }
       ^

```

Operator member syntax doesn't solve ambiguity issues. It just provides a way to reference member operators.

---

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [December 18, 2019, 1:14am UTC](https://forums.swift.org/t/operator-member-syntax/31773/8 "2019-12-18T01:14:22Z")

</div>

> [@t-ae](#):
>
> (Or what you intended to link is this?: [https://developer.apple.com/documentation/swift/simd/3139462](https://developer.apple.com/documentation/swift/simd/3139462))

That was indeed the intended link, thanks!

Since `SIMD.`.<`` has three overloads:

- [`(Self, Self) -> SIMDMask<Self.MaskStorage>`](https://developer.apple.com/documentation/swift/simd/3139462)
- [`(Self.Scalar, Self) -> SIMDMask<Self.MaskStorage>`](https://developer.apple.com/documentation/swift/simd/3139460)
- [`(Self, Self.Scalar) -> SIMDMask<Self.MaskStorage>`](https://developer.apple.com/documentation/swift/simd/3139461)

The following should be ambiguous without a contextual type:

```swift
_ = SIMD.`.<`

```

---

<div class="post-metadata">

**Author:** ![rxwei](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rxwei/32/664_2.png) [@rxwei](https://forums.swift.org/u/rxwei)\
**Post date:** [December 18, 2019, 1:20am UTC](https://forums.swift.org/t/operator-member-syntax/31773/9 "2019-12-18T01:20:42Z")

</div>

The qualified operator lookup syntax does not solve the overload ambiguity problem. Qualified name lookup for non-operators does not solve that problem either. Thus I think overload ambiguity is almost entirely orthogonal to the operator member syntax proposed here.

There are two reasons qualified operator lookup is desirable:

1. This always seems like a missing feature, because qualified reference to a static non-operator member is already possible.

2. It improves clarity for potential language features that retroactively provide a behavior to an existing method. This includes dynamic replacement and differentiable programming.

---

<div class="post-metadata">

**Author:** ![t-ae](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/t-ae/32/3755_2.png) [@t-ae](https://forums.swift.org/u/t-ae)\
**Post date:** [December 18, 2019, 1:40am UTC](https://forums.swift.org/t/operator-member-syntax/31773/10 "2019-12-18T01:40:06Z")

</div>

> [@dan-zheng](#):
>
> If a member operator is overloaded for the same parameter arity and parameter labels, then direct references to it with no contextual type would be truly ambiguous.

It's not what I want to say (It's my sentense which was truly ambiguous).

What I wanted to remark was that how to refer function `+(_: A, _: B) -> C` in proposed syntax.  
Which type has this function as a member (`A.+` or `B.+` or `C.+`)?

I think the word `member operator` is not well defined.

---

<div class="post-metadata">

**Author:** ![dan-zheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dan-zheng/32/2534_2.png) [@dan-zheng](https://forums.swift.org/u/dan-zheng)\
**Post date:** [December 18, 2019, 1:59am UTC](https://forums.swift.org/t/operator-member-syntax/31773/11 "2019-12-18T01:59:00Z")

</div>

> [@t-ae](#):
>
> What I wanted to remark was that how to refer function `+(_: A, _: B) -> C` in proposed syntax.  
> Which type has this function as a member ( `A.+` or `B.+` or `C.+` )?

Good question. Operator member syntax is relevant only for operators declared as static methods of a particular type, not for operators declared as top-level functions.

```swift
infix operator +++

// This operator is a top-level function, not a member of any type.
func +++(_ x: Int, y: Int) -> Int {
  x + y
}

extension Int {
  // This operator is a static member of `Int`.
  // The discussion involves these kinds of operators.
  static func +++(_ x: Int, y: Int) -> Int {
    x + y
  }
}

// Operator member syntax.
_ = Int.`+++`

```

---

<div class="post-metadata">

**Author:** ![rxwei](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rxwei/32/664_2.png) [@rxwei](https://forums.swift.org/u/rxwei)\
**Post date:** [December 18, 2019, 2:13am UTC](https://forums.swift.org/t/operator-member-syntax/31773/12 "2019-12-18T02:13:42Z")

</div>

> [@t-ae](#):
>
> What I wanted to remark was that how to refer function `+(_: A, _: B) -> C` in proposed syntax.  
> Which type has this function as a member ( `A.+` or `B.+` or `C.+` )?

Depends on where this operator is defined. If it is a top-level operator, the only possible qualified reference prefix would be the module name. If it is a member of type `Foo`, then a qualified name can be `Foo.+` or `ModuleName.Foo.+`. In other words, this should be consistent with qualified lookup for non-operators, IMO.

---

<div class="post-metadata">

**Author:** ![beccadax](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/beccadax/32/34217_2.png) [@beccadax](https://forums.swift.org/u/beccadax)\
**Post date:** [December 18, 2019, 7:56am UTC](https://forums.swift.org/t/operator-member-syntax/31773/13 "2019-12-18T07:56:00Z")

</div>

> [@Lantua](#):
>
> prefix/suffix variants

I’ve actually thought for a while that we ought to treat prefix operators like their parameter has the label `prefix:` and postfix operators like `postfix:` (names subject to bikeshedding). Then `+(prefix:)`, `+(_:_:)`, and `+(postfix:)` would be unambiguous.

---

<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:** [December 18, 2019, 12:52pm UTC](https://forums.swift.org/t/operator-member-syntax/31773/14 "2019-12-18T12:52:48Z")

</div>

Yes please!!!

> [@dan-zheng](#):
>
> It's currently not supported, but I wonder if it's just a parser deficiency

I once asked this question and received this as an answer:

> [@Why are operators not available as static type members?](https://forums.swift.org/t/why-are-operators-not-available-as-static-type-members/19387/2):
>
> Parsing is pretty much the only reason I know of. I think we'd have to always parenthesize it in case someone defines .+ as a postfix operator that applies to types (even though you're supposed to write that Int.self.+), but accepting the parenthesized form seems like it'd be a fairly non-controversial evolution proposal, and it's probably not too hard for someone to implement. Another possible spelling: Int.`+` and Int.`.+`. I might like this one even better because it doesn't even really need…

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [December 18, 2019, 4:05pm UTC](https://forums.swift.org/t/operator-member-syntax/31773/15 "2019-12-18T16:05:41Z")

</div>

Yeah, I think so as well. There's a [bug](https://bugs.swift.org/browse/SR-5509) where parsing `[>, <]` fails and you have to wrap it in parens (i.e. `[(>), (<)]`, so that's what we'd have to do here as well i.e. `Float.(+)` to access it.
