# \#operators

**URL:** https://forums.swift.org/tag/operators/72.md

[Latest](https://forums.swift.org/latest.md) · [Categories](https://forums.swift.org/categories.md) · [Tags](https://forums.swift.org/tags.md)

---

## [Strange issue with custom "+" operator](https://forums.swift.org/t/strange-issue-with-custom-operator/85073)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 2\
**Last updated:** [March 2, 2026, 2:18am UTC](https://forums.swift.org/t/strange-issue-with-custom-operator/85073 "2026-03-02T02:18:00Z")

</div>

This doesn't compile, but why? struct X {} struct Y {} let x = X() infix operator + : AdditionPrecedence // tried without it func + (a: X, b: X) -\> Y { Y() } func + (a: Y, b: Y) -\> Y { Y() } let y: Y = x + x // ✅ \_ = …

---

## [Bitwise shifting by a negative number](https://forums.swift.org/t/bitwise-shifting-by-a-negative-number/77622)

<div class="topic-metadata">

**Author:** [@iperkard](https://forums.swift.org/u/iperkard)\
**Replies:** 2\
**Last updated:** [February 3, 2025, 10:48am UTC](https://forums.swift.org/t/bitwise-shifting-by-a-negative-number/77622 "2025-02-03T10:48:14Z")

</div>

Hi there, A rapid test in Xcode shows that bitwise shifting by a negative number seems to reverse the direction of the shift. I didn't find any confirmation of this behaviour (wich is undefined in C for example), even …

---

## [Short-circuiting assignment operators](https://forums.swift.org/t/short-circuiting-assignment-operators/72794)

<div class="topic-metadata">

**Author:** [@rauhul](https://forums.swift.org/u/rauhul)\
**Replies:** 1\
**Last updated:** [June 27, 2024, 10:39pm UTC](https://forums.swift.org/t/short-circuiting-assignment-operators/72794 "2024-06-27T22:39:04Z")

</div>

Hi all, apologies if this has been pitched before... I wanted to propose adding three short-circuiting assignment operators I frequently find useful in my code to the standard library. Nil-Coalescing Assignment Operato…

---

## [Integer comparisons via bit operations](https://forums.swift.org/t/integer-comparisons-via-bit-operations/71664)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 6\
**Last updated:** [May 8, 2024, 1:58am UTC](https://forums.swift.org/t/integer-comparisons-via-bit-operations/71664 "2024-05-08T01:58:18Z")

</div>

I am looking for a way to express the following: func compare(x: UInt64, y: UInt64) -\> UInt64 { x \< y ? 1 : 0 } without using comparisons but only using: arithmetic &+, &- bit operations: &, |, ^, ~, \>\>, \<\< casts be…

---

## [Subclass operator](https://forums.swift.org/t/subclass-operator/70257)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 3\
**Last updated:** [February 25, 2024, 4:00am UTC](https://forums.swift.org/t/subclass-operator/70257 "2024-02-25T04:00:53Z")

</div>

Found an interesting corner in Swift: infix operator \<-\> class Base { static func \<-\>(\_ lhs: Base, \_ rhs: Base) -\> Int { print("base implementation"); return 0 } } class Derived: Base { static func \<-…

---

## [Strange error in operator overload resolution](https://forums.swift.org/t/strange-error-in-operator-overload-resolution/66583)

<div class="topic-metadata">

**Author:** [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Replies:** 2\
**Last updated:** [August 4, 2023, 5:29pm UTC](https://forums.swift.org/t/strange-error-in-operator-overload-resolution/66583 "2023-08-04T17:29:01Z")

</div>

Continuing from Associative arithmetic operation order matters in Swift? - #8 by tera. The MRE is merely: struct Foo { static func + (lhs: Foo, rhs: Foo) -\> Foo { .init() } } struct Bar { static func + (lhs: Ba…

---

## [Operator alias](https://forums.swift.org/t/operator-alias/65957)

<div class="topic-metadata">

**Author:** [@DanL](https://forums.swift.org/u/DanL)\
**Replies:** 6\
**Last updated:** [July 3, 2023, 6:25pm UTC](https://forums.swift.org/t/operator-alias/65957 "2023-07-03T18:25:35Z")

</div>

Let's say we have an operator like this: prefix operator \<\> extension Optional { static prefix func \<\>(opt: Optional\<Wrapped\>) -\> Bool { return opt != nil } } It allows us to write: if \<\>someOptional { …

---

## [How to turn a int operator result into Double (Float) value?](https://forums.swift.org/t/how-to-turn-a-int-operator-result-into-double-float-value/65333)

<div class="topic-metadata">

**Author:** [@Mrsahin888](https://forums.swift.org/u/Mrsahin888)\
**Replies:** 1\
**Last updated:** [June 2, 2023, 6:23pm UTC](https://forums.swift.org/t/how-to-turn-a-int-operator-result-into-double-float-value/65333 "2023-06-02T18:23:53Z")

</div>

Hello. I just started learning Swift. I'm going step by step. There is one point I don't understand. Can anyone assist on this? From line 98 I defined two numbers (number10 and number11) These numbers (I am giving an…

---

## [Gotchas related to prefix +/- operators](https://forums.swift.org/t/gotchas-related-to-prefix-operators/63086)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 0\
**Last updated:** [February 12, 2023, 6:41am UTC](https://forums.swift.org/t/gotchas-related-to-prefix-operators/63086 "2023-02-12T06:41:11Z")

</div>

Let's have a place to discuss issues related to prefix plus and prefix minus operators gotchas (unexpected behaviour) cause by prefix minus being parsed as part of the integer and float literal processing and due to othe…

---

## ["... and assign" operators alternative](https://forums.swift.org/t/and-assign-operators-alternative/62609)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 1\
**Last updated:** [January 17, 2023, 5:30pm UTC](https://forums.swift.org/t/and-assign-operators-alternative/62609 "2023-01-17T17:30:28Z")

</div>

Swift has taken += and similar operators from C. There's quite a few of those "... and assign" operators: \*= /= %= += -= \<\<= \>\>= &= |= ^= &\*= &+= &-= &\<\<= &\>\>= .&= .|= .^= These operators, however ugly th…

---

## [Overloading operator ()](https://forums.swift.org/t/overloading-operator/14155)

<div class="topic-metadata">

**Author:** [@medismailben](https://forums.swift.org/u/medismailben)\
**Replies:** 11\
**Last updated:** [December 4, 2022, 11:28pm UTC](https://forums.swift.org/t/overloading-operator/14155 "2022-12-04T23:28:29Z")

</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 o…

---

## [Await keyword is no member of the "Language reference/Lexical Structure/Keywords and Punctuation"](https://forums.swift.org/t/await-keyword-is-no-member-of-the-language-reference-lexical-structure-keywords-and-punctuation/60572)

<div class="topic-metadata">

**Author:** [@brecsoks](https://forums.swift.org/u/brecsoks)\
**Replies:** 5\
**Last updated:** [September 30, 2022, 11:40pm UTC](https://forums.swift.org/t/await-keyword-is-no-member-of-the-language-reference-lexical-structure-keywords-and-punctuation/60572 "2022-09-30T23:40:30Z")

</div>

Hi Team! I am writing a Backus–Naur form reference for myself. I realized that the keyword for the newly available await-operator → await operator is not in any category of the "Language reference/Lexical Structure/Ke…

---

## [Operator default parameters](https://forums.swift.org/t/operator-default-parameters/58264)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 2\
**Last updated:** [June 19, 2022, 2:13am UTC](https://forums.swift.org/t/operator-default-parameters/58264 "2022-06-19T02:13:12Z")

</div>

Should default values be prohibited here? infix operator \*\* func \*\*(num: Double = 1, n: Double = 2) -\> Double { pow(num, n) } or should they work? Straw man syntax: 123.4 \*\* default

---

## [Custom Bool operator warning issue](https://forums.swift.org/t/custom-bool-operator-warning-issue/55002)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 0\
**Last updated:** [February 1, 2022, 1:39am UTC](https://forums.swift.org/t/custom-bool-operator-warning-issue/55002 "2022-02-01T01:39:53Z")

</div>

To experiment with keeping my app "explanation mark free" I've created my own analogues for != and ! operators some time ago (don't want to go into further details on this, ask me privately if you want to know what "expl…

---

## [Division and prefix minus with overflow](https://forums.swift.org/t/division-and-prefix-minus-with-overflow/50195)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 15\
**Last updated:** [November 4, 2021, 2:14pm UTC](https://forums.swift.org/t/division-and-prefix-minus-with-overflow/50195 "2021-11-04T14:14:14Z")

</div>

swift defines these three: \* Overflow addition ( \`&+\` ) \* Overflow subtraction ( \`&-\` ) \* Overflow multiplication ( \`&\*\` ) but not the fourth for division. an oversight? usage example: func divide(x: Int8, y: Int8) -\> …

---

## [Can't use try? after custom infix operator in result builder](https://forums.swift.org/t/cant-use-try-after-custom-infix-operator-in-result-builder/52443)

<div class="topic-metadata">

**Author:** [@crichez](https://forums.swift.org/u/crichez)\
**Replies:** 1\
**Last updated:** [September 29, 2021, 1:39pm UTC](https://forums.swift.org/t/cant-use-try-after-custom-infix-operator-in-result-builder/52443 "2021-09-29T13:39:23Z")

</div>

I made a result builder that turns key-value pairs into a dictionary. I'm getting a compiler exception when I build a test that uses that result builder in combination with a custom infix operator and a try? statement. N…

---

## [A question about use 'lowerThan' in 'precedencegroup', maybe is a bug](https://forums.swift.org/t/a-question-about-use-lowerthan-in-precedencegroup-maybe-is-a-bug/51601)

<div class="topic-metadata">

**Author:** [@Lowinding](https://forums.swift.org/u/Lowinding)\
**Replies:** 1\
**Last updated:** [August 30, 2021, 8:51am UTC](https://forums.swift.org/t/a-question-about-use-lowerthan-in-precedencegroup-maybe-is-a-bug/51601 "2021-08-30T08:51:58Z")

</div>

I hope I'm wrong. I turned to developer.apple.com, a people suggested me: Could be worth a bug report on documentation. But I still want to confirm, and I don’t know how to correctly submit the bug report on documentatio…

---

## [Range Operators and Negative Numbers](https://forums.swift.org/t/range-operators-and-negative-numbers/50842)

<div class="topic-metadata">

**Author:** [@mmmayer](https://forums.swift.org/u/mmmayer)\
**Replies:** 7\
**Last updated:** [August 3, 2021, 7:24am UTC](https://forums.swift.org/t/range-operators-and-negative-numbers/50842 "2021-08-03T07:24:24Z")

</div>

My apologies if this has been asked before (I didn't find it in search), but what are the reasons for needing to put parenthesis around the trailing value of a range operator if it is negative (unary minus operator). Exa…

---

## [How do I pass an operator as a closure outside of an argument?](https://forums.swift.org/t/how-do-i-pass-an-operator-as-a-closure-outside-of-an-argument/50057)

<div class="topic-metadata">

**Author:** [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Replies:** 2\
**Last updated:** [June 27, 2021, 10:58pm UTC](https://forums.swift.org/t/how-do-i-pass-an-operator-as-a-closure-outside-of-an-argument/50057 "2021-06-27T22:58:19Z")

</div>

I'm trying out code like: guard areIndicesForMinima ? element \> subRoot : element \< subRoot else { continue } and wanted to compact the two comparisons into a single closure. But I can't figure out how to express th…

---

## [Swift - Overloading “+” Operator - “Cannot Convert Value of type” Erro](https://forums.swift.org/t/swift-overloading-operator-cannot-convert-value-of-type-erro/49926)

<div class="topic-metadata">

**Author:** [@popwarfour](https://forums.swift.org/u/popwarfour)\
**Replies:** 3\
**Last updated:** [June 25, 2021, 9:03pm UTC](https://forums.swift.org/t/swift-overloading-operator-cannot-convert-value-of-type-erro/49926 "2021-06-25T21:03:53Z")

</div>

I'm hitting a compiler error that is best illustrated by the small code snippet below. My goal is be able to perform addition on instances of MyEnum directly along side CGFloat. Now for simple cases this works perfect…

---

## [Custom Operator Assignment in Initialization](https://forums.swift.org/t/custom-operator-assignment-in-initialization/44592)

<div class="topic-metadata">

**Author:** [@Freytag](https://forums.swift.org/u/Freytag)\
**Replies:** 5\
**Last updated:** [February 11, 2021, 10:17am UTC](https://forums.swift.org/t/custom-operator-assignment-in-initialization/44592 "2021-02-11T10:17:33Z")

</div>

Hey, everyone. I have a wrapper type Observable\<Value\> wherein the wrapped value is kept in a base property, very similar to the AnyHashable wrapper type. A custom set of operators beginning with "^^" are defined as well…

---

## [Add XOR (Exclusive OR) operator support for Bool type](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346)

<div class="topic-metadata">

**Author:** [@Jonathan-Hoch](https://forums.swift.org/u/Jonathan-Hoch)\
**Replies:** 27\
**Last updated:** [October 23, 2020, 9:53pm UTC](https://forums.swift.org/t/add-xor-exclusive-or-operator-support-for-bool-type/41346 "2020-10-23T21:53:15Z")

</div>

The ^ (XOR - Exclusive OR) operator is currently not supported for Bool type. It is commonly used in other languages such as C and Java. I propose adding support via an extension. So, Instead of having to write someth…

---

## [Precedence/associativity of member access and function call](https://forums.swift.org/t/precedence-associativity-of-member-access-and-function-call/41212)

<div class="topic-metadata">

**Author:** [@woolsweater](https://forums.swift.org/u/woolsweater)\
**Replies:** 4\
**Last updated:** [October 16, 2020, 5:17pm UTC](https://forums.swift.org/t/precedence-associativity-of-member-access-and-function-call/41212 "2020-10-16T17:17:39Z")

</div>

Do the . member access and () function call syntactical elements have defined precedence and associativity that's written down in a doc somewhere? They are not in the official list of operators linked from the language r…

---

## [Operators conform to protocols?](https://forums.swift.org/t/operators-conform-to-protocols/36767)

<div class="topic-metadata">

**Author:** [@wowbagger](https://forums.swift.org/u/wowbagger)\
**Replies:** 2\
**Last updated:** [May 22, 2020, 5:08pm UTC](https://forums.swift.org/t/operators-conform-to-protocols/36767 "2020-05-22T17:08:34Z")

</div>

I see that operator declarations use this syntax in the standard library: fixity operator: PrecedenceGroup, Protocol. This is undocumented. Is this some kind of protocol conformance or inheritance?

---

## [On operator precedences and missed opportunities](https://forums.swift.org/t/on-operator-precedences-and-missed-opportunities/31341)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 10\
**Last updated:** [December 7, 2019, 7:58pm UTC](https://forums.swift.org/t/on-operator-precedences-and-missed-opportunities/31341 "2019-12-07T19:58:00Z")

</div>

this is a bit of a historic investigation i want to share with you. not a pitch, or anything, i do not expect any changes to swift language in that area. this is an observation of what had happened and what could have ha…

---

## [Custom comparison operator](https://forums.swift.org/t/custom-comparison-operator/31227)

<div class="topic-metadata">

**Author:** [@tera](https://forums.swift.org/u/tera)\
**Replies:** 19\
**Last updated:** [December 3, 2019, 9:57pm UTC](https://forums.swift.org/t/custom-comparison-operator/31227 "2019-12-03T21:57:29Z")

</div>

is it possible to define your own comparison operator? shall be easy, but how exactly? my attempt: infix operator \<\> : ComparisonPrecedence func \<\> \<T: Equatable\>(a: T?, b: T?) -\> Bool { return a != b } func test(a…

---

## [Operator Declarations](https://forums.swift.org/t/operator-declarations/26082)

<div class="topic-metadata">

**Author:** [@Wildchild9](https://forums.swift.org/u/Wildchild9)\
**Replies:** 1\
**Last updated:** [June 20, 2019, 6:13am UTC](https://forums.swift.org/t/operator-declarations/26082 "2019-06-20T06:13:13Z")

</div>

I don't know if it is just me, but precedence group declarations seem very much out of place in their current state in Swift. precedencegroup precedence group name { higherThan: lower group names lowerThan: h…

---

## [Comparing Optionals with each other](https://forums.swift.org/t/comparing-optionals-with-each-other/22444)

<div class="topic-metadata">

**Author:** [@sharedRoutine](https://forums.swift.org/u/sharedRoutine)\
**Replies:** 5\
**Last updated:** [April 1, 2019, 3:25pm UTC](https://forums.swift.org/t/comparing-optionals-with-each-other/22444 "2019-04-01T15:25:29Z")

</div>

Hi, recently I've programmed a lot of Javascript. And in JS the expressions I wanted to be evaluated were something like: expirationDate \< new Date(); Which is expirationDate is undefined returns false (which is the i…

---

## [Prefix and postfix operators not working for tuple types](https://forums.swift.org/t/prefix-and-postfix-operators-not-working-for-tuple-types/17466)

<div class="topic-metadata">

**Author:** [@Jens](https://forums.swift.org/u/Jens)\
**Replies:** 5\
**Last updated:** [November 5, 2018, 10:22pm UTC](https://forums.swift.org/t/prefix-and-postfix-operators-not-working-for-tuple-types/17466 "2018-11-05T22:22:26Z")

</div>

Are these errors caused by a known compiler bug (in Xcode 10): infix operator ||| prefix operator \<\<\< postfix operator \>\>\> func |||(lhs: (Int, Int), rhs: (Int, Int)) -\> Void { print("infix") } prefix func \<\<\<(rhs: (In…

---

## [Allow an underscore (\_) on the right side of the Ternary (?: ) and the Nil-Coalescing (? ?) operators](https://forums.swift.org/t/allow-an-underscore-on-the-right-side-of-the-ternary-and-the-nil-coalescing-operators/12511)

<div class="topic-metadata">

**Author:** [@SubjugaterCephalopod](https://forums.swift.org/u/SubjugaterCephalopod)\
**Replies:** 13\
**Last updated:** [May 28, 2018, 11:16am UTC](https://forums.swift.org/t/allow-an-underscore-on-the-right-side-of-the-ternary-and-the-nil-coalescing-operators/12511 "2018-05-28T11:16:42Z")

</div>

It would reduce some common if clutter without making the code harder to understand, or conflicting with unary operators. Examples: something = optionalThing ?? \_ optionaThing != nil ? doSomething() : \_ The abo…

[Next page](https://forums.swift.org/tag/operators/72.md?match_all_tags=true&page=1&tags%5B%5D=operators)
