Back to Carbon Lang

Principle: One static open extension mechanism

proposals/p0998.md

0.0.0-0.nightly.2026.05.062.0 KB
Original Source

Principle: One static open extension mechanism

<!-- Part of the Carbon Language project, under the Apache License v2.0 with LLVM Exceptions. See /LICENSE for license information. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -->

Pull request

<!-- toc -->

Table of contents

<!-- tocstop -->

Problem

There are a few ways of approaching open extension, such as defining how operators are overload for every type. For Carbon, with its focus on performance, we are particularly interested in those that support static dispatch, to avoid the runtime overhead of dynamic dispatch. The three main options are:

  • Open function overloading, where new overloads for a given name can be defined broadly, as is done in C++.
  • Interfaces, as is done in Rust.
  • Special method names, as are used in C++ and Python.

We would prefer to use a single mechanism, if possible, for simplicity.

Proposal

Proposal is to use interfaces as the single open extension mechanism.

Details

Details are in the added principle doc: docs/project/principles/static_open_extension.md.

Rationale based on Carbon's goals

This proposal is pursuing Carbon's goal of having code that is easy to read, understand, and write.

Alternatives considered

Early arguments for this approach were put forth in this document.