Английская Википедия:Carbon (programming language)

Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску

Шаблон:Short description

Шаблон:Use dmy dates Шаблон:Infobox programming language

Carbon is an experimental programming language designed for interoperability with C++.[1] The project is open-source and was started at Google. Google engineer Chandler Carruth first introduced Carbon at the CppNorth conference in Toronto in July 2022. He stated that Carbon was created to be a C++ successor.[2][3][4] The language is expected to have an experimental MVP version 0.1 in 2025 and a production-ready version 1.0 after 2027.[5]

The language intends to fix several perceived shortcomings of C++[6] but otherwise provides a similar feature set. The main goals of the language are readability and "bi-directional interoperability" (which allows the user to include C++ code in the Carbon file), as opposed to using a new language like Rust, that, while being influenced by C++, is not two-way compatible with C++ programs. Changes to the language will be decided by the Carbon leads.[7][8][9][10]

Carbon's documents, design, implementation, and related tools are hosted on GitHub under the Apache-2.0 license with LLVM Exceptions.[11]

Example

The following shows how a program might be written in Carbon and C++:[12]

Carbon C++
package Geometry api;
import Math;

class Circle {
  var r: f32;
}

fn PrintTotalArea(circles: Slice(Circle)) {
  var area: f32 = 0;
  for (c: Circle in circles) {
    area += Math.Pi * c.r * c.r;
  }
  Print("Total area: {0}", area);
}

fn Main() -> i32 {
  // A dynamically sized array, like `std::vector`.
  var circles: Array(Circle) = ({.r = 1.0}, {.r = 2.0});
  // Implicitly converts `Array` to `Slice`.
  PrintTotalArea(circles);
  return 0;
}
#include <math.h>
#include <iostream>
#include <span>
#include <vector>

struct Circle {
  float r;
};

void PrintTotalArea(std::span<Circle> circles) {
  float area = 0;
  for (const Circle& c : circles) {
    area += M_PI * c.r * c.r;
  }
  std::cout << "Total area: " << area << "\n";
}

auto main(int argc, char** argv) -> int {
  std::vector<Circle> circles = {{1.0}, {2.0}};
  // Implicitly converts `vector` to `span`.
  PrintTotalArea(circles);
  return 0;
}

See also

Шаблон:Portal

References

Шаблон:Reflist

External links

Шаблон:Google FOSS Шаблон:Google LLC


Шаблон:Google-stub

  1. Шаблон:Cite web
  2. Ошибка цитирования Неверный тег <ref>; для сносок cppnorthkeynote-sched не указан текст
  3. Ошибка цитирования Неверный тег <ref>; для сносок cppnorthkeynote-video не указан текст
  4. Ошибка цитирования Неверный тег <ref>; для сносок cppnorthkeynote-9to5google не указан текст
  5. Шаблон:Citation
  6. Ошибка цитирования Неверный тег <ref>; для сносок difficulties improving cpp не указан текст
  7. Ошибка цитирования Неверный тег <ref>; для сносок c1 не указан текст
  8. Ошибка цитирования Неверный тег <ref>; для сносок c2 не указан текст
  9. Ошибка цитирования Неверный тег <ref>; для сносок c3 не указан текст
  10. Ошибка цитирования Неверный тег <ref>; для сносок c4 не указан текст
  11. Шаблон:Cite web
  12. Шаблон:Cite web