As the lead of a shared Swift codebase, would you allow custom operators, and what rule would you write for when one is acceptable?
answer
- conformance versus new glyph
- no name to search for
- declarations travel with imports
- a named method twin
basics
~20 sAllow overloading standard operators where the meaning carries over, such as + on vectors or == through Equatable, and keep brand-new operators rare: only for a domain with established notation, each with a named method, an explicit precedencegroup and one owning module.
solid answer
~50 sI would separate overloading from inventing. Conformances such as `Equatable`, `Comparable` and `AdditiveArithmetic`, and overloads like `+` on a vector, keep the standard meaning and precedence, so I encourage them. A brand-new operator costs more than it looks: it has no name to search for, its precedence is invisible at the call site, a missing group leaves it in `DefaultPrecedence` where it needs parentheses beside `+` or `==`, and its declaration takes no access modifier and reaches every importing module, so two modules declaring it differently produce "ambiguous operator declarations found for operator". Heavy overloading also slows type-checking of long expressions. My rule: new operators only where the domain already uses the notation, such as vector math or a parser DSL, declared in one owning module, with an explicit, tested `precedencegroup` and a named method beside each one.
go deeper
Recall that conforming to Equatable or Comparable gives a type standard operators, and that a brand-new operator needs its own declaration.
Explain why a new operator is harder to read and search than a method, and what goes wrong when it is declared without a precedence group.
Review operator pull requests on substance: does a named method say it as clearly, is the precedence explicit and tested, and which modules will see it.
Own the rule: keep conformances and familiar overloads easy, make new operators rare, owned and named, and weigh notation gains against discoverability and type-checking cost.
## What the language makes cheap Swift lets any module declare a new operator in one line and give it a precedence group in a few more. That makes two very different things look equally easy: | Choice | Example | Reader cost | Typical verdict | |---|---|---|---| | Conformance to a standard protocol | `Equatable` gives `==`, `Comparable` gives `<`, `AdditiveArithmetic` gives `+` | none: the meaning is the standard one | encourage | | Overloading an existing operator with its usual meaning | `+` on `Vector2D`, `*` for scaling | low: the glyph and precedence are known | allow | | Overloading an existing operator with a new meaning | `+` on `String` that inserts a space | high: the familiar glyph now lies | reject | | A brand-new operator | `•` for dot product, `\|>` for piping | high: new glyph, new precedence, new search term | allow only by rule | A lead's job is to keep the first two cheap and make the last two deliberate. ## The costs a team pays for a new operator - **Discoverability.** A symbol has no name to search for in code review or documentation, and a newcomer reading `a <~> b` cannot guess what it does. - **Precedence is invisible.** A reader must know the operator's group to parse `a • b + c`. Forgetting a group altogether gives the operator `DefaultPrecedence`, which is unordered against `+`, `==` and most other groups, so ordinary expressions need parentheses. - **Operators are module-wide and travel with imports.** An operator declaration takes no access modifier: `public infix operator` is rejected with `unexpected attribute 'public' in operator declaration`, and the declaration is visible to every module that imports yours. If two imported modules declare the same operator in different groups, a use of it fails with `ambiguous operator declarations found for operator`. - **Type-checking cost.** Every overload of a common operator adds candidates the type checker must consider. Long expressions mixing heavily overloaded operators and literals are a known source of `the compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions`. ## When a new operator earns its place The honest case for a custom operator is a **domain with established notation** where the symbolic form is easier to check than the named one: - vector and matrix math in a geometry or graphics module; - parser combinators or a small layout or query language, where operators make the grammar read like the domain; - a symbol that experts in that domain already read fluently. Convenience for one author, or saving a few characters of a method call, is not that case. ## A rule you can write down 1. **Prefer conformances.** If a standard protocol gives the operator its meaning (`Equatable`, `Comparable`, `AdditiveArithmetic`), conform rather than hand-roll. 2. **Overload existing operators only with their usual meaning.** `+` combines, `*` scales, `==` compares; nothing else. 3. **New operators need an owner module and an approval.** Declare each in exactly one module, never in a shared utilities file, and list it in that module's documentation. 4. **Always give a precedence group,** placed relative to the standard groups and covered by a test of the expressions it must parse and the ones it must reject. 5. **Pair every operator with a named method** (`dot(_:)` beside `•`), so call sites can choose clarity and search finds the behaviour. 6. **No glyph that readers already associate with a different meaning or precedence,** because the familiar reading wins over the documented one. ## Reviewing an operator pull request - Does a named method say the same thing just as clearly? If so, ask for the method. - Is the precedence group explicit, and do the tests show `a • b + c` parsing the way the author intends? - Will the declaration reach modules that never asked for it? - Would the operator still be obvious to someone joining the team next year? ## The trade-off in one line Operators buy density for readers who already know the notation and charge everyone else in discoverability and type-checking time. A rule that keeps conformances and familiar overloads easy, and makes new symbols rare, owned and named, captures most of the benefit at a fraction of the cost.
- A teammate adds `infix operator |>` to a shared utilities module for piping values into functions. What do you say in review?I would ask what it buys over method chaining or a named `pipe` helper, because every module importing utilities now sees the operator and could collide with another declaration of it. If the team still wants it, it needs an explicit precedence group, tests for how it mixes with other operators, and a home in the module that actually uses it.
- How would you enforce the rule rather than rely on reviewers remembering it?Put it in the team's Swift style guide, and have a lint step or a review checklist flag new `operator` and `precedencegroup` declarations, which are easy to search for because they always sit at file scope. Each approved operator then gets listed in its module's documentation with its named-method twin.
- A custom operator is private to the file that declares it
- Operator overloads never affect compile times
- If a glyph reads well to its author it will read well to the team
- Forbidding custom operators also forbids == or < on our own types
- A new operator needs no named method, since the symbol documents itself