Skip to content

Commit

Permalink
Generic details 11: operator overloading (#1144)
Browse files Browse the repository at this point in the history
Operators rewrite to calls of specific operator interface functions, so you overload an operator for a type by implementing an interface for it. There is a `like` operator for defining a set if implementations for supporting implicit conversions more conveniently.

Co-authored-by: Geoff Romer <gromer@google.com>
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
  • Loading branch information
3 people authored Apr 8, 2022
1 parent 9d0f55a commit db66a43
Show file tree
Hide file tree
Showing 5 changed files with 536 additions and 16 deletions.
20 changes: 12 additions & 8 deletions docs/design/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- [Friends](#friends)
- [Test friendship](#test-friendship)
- [Access control for construction](#access-control-for-construction)
- [Operator overloading](#operator-overloading)
- [Future work](#future-work)
- [Struct literal shortcut](#struct-literal-shortcut)
- [Optional named parameters](#optional-named-parameters)
- [Field defaults for struct types](#field-defaults-for-struct-types)
- [Destructuring in pattern matching](#destructuring-in-pattern-matching)
- [Discussion](#discussion)
- [Operator overloading](#operator-overloading)
- [Inheritance](#inheritance-1)
- [Destructors](#destructors)
- [C++ abstract base classes interoperating with object-safe interfaces](#c-abstract-base-classes-interoperating-with-object-safe-interfaces)
Expand Down Expand Up @@ -1548,6 +1548,17 @@ if it has access to (write) all of its fields.
even when it only has public fields. This will be resolved in question-for-leads
issue [#803](https://github.com/carbon-language/carbon-lang/issues/803).

### Operator overloading

Developers may define how standard Carbon operators, such as `+` and `/`, apply
to custom types by implementing the
[interface](generics/terminology.md#interface) that corresponds to that operator
for the types of the operands. See the
["operator overloading" section](generics/details.md#operator-overloading) of
the [generics design](generics/overview.md). The specific interface used for a
given operator may be found in the
[expressions design](/docs/design/expressions/README.md).

## Future work

This includes features that need to be designed, questions to answer, and a
Expand Down Expand Up @@ -1636,13 +1647,6 @@ Some discussion on this topic has occurred in:
[2](https://docs.google.com/document/d/1u6GORSkcgThMAiYKOqsgALcEviEtcghGb5TTVT-U-N0/edit)
- ["match" in syntax choices doc](https://docs.google.com/document/d/1iuytei37LPg_tEd6xe-O6P_bpN7TIbEjNtFMLYW2Nno/edit#heading=h.y566d16ivoy2)

### Operator overloading

This includes destructors, copy and move operations, as well as other Carbon
operators such as `+` and `/`. We expect types to implement these operations by
implementing corresponding interfaces, see
[the generics overview](generics/overview.md).

### Inheritance

#### Destructors
Expand Down
Loading

0 comments on commit db66a43

Please sign in to comment.