谷歌发布开源开发语言 Carbon : 号称将替代 C++

2022-07-20 21:22:10 +08:00
 charlieethan

项目的 Github 地址为: https://github.com/carbon-language/carbon-lang

在近日举行的 CppNorth 开发者大会上,谷歌工程师 Chandler Carruth 宣布了名为 “Carbon” 的全新开源开发语言,并称它将是 C++ 的继任者。Chandler Carruth 表示,Carbon 拥有与 C++ 的“双向互操作性”,也就是说开发者可以直接在 Carbon 语言的程序中使用 C++,这大大提升了项目迁移的便捷性。

谷歌在开发该语言的时候,就将接替 C++ 作为了核心目标,它拥有大量与 C++ 相契合的特性,一个熟练的 C++ 开发者将能够迅速上手 Carbon ,并熟练进行程序的编辑

C++

// C++:
#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 constructors `span` from `vector`.
  PrintTotalArea(circles);
  return 0;
}

Carbon

// Carbon:
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 constructs `Slice` from `Array`.
  PrintTotalArea(circles);
  return 0;
}

Mixed

// C++ code used in both Carbon and C++:
struct Circle {
  float r;
};

// Carbon exposing a function for C++:
package Geometry api;
import Cpp library "circle.h";
import Math;

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

// C++ calling Carbon:
#include <vector>
#include "circle.h"
#include "geometry.carbon.h"

auto main(int argc, char** argv) -> int {
  std::vector<Circle> circles = {{1.0}, {2.0}};
  // Carbon's `Slice` supports implicit construction from `std::vector`,
  // similar to `std::span`.
  Geometry::PrintTotalArea(circles);
  return 0;
}
15982 次点击
所在节点    C++
110 条回复
XIVN1987
2022-07-21 22:15:52 +08:00
虽然好多人不看好 Carbon ,但考虑到 Google 的超能力,,说不定比 Rust 更流行也未可知。。
w3cll
2022-07-21 22:32:09 +08:00
Google 都有好多个语言了,能不能把一个搞好
wsseo
2022-07-21 22:53:29 +08:00
没意思,期待华为的语言
Aloento
2022-07-22 05:46:00 +08:00
考虑一下 Zig 吧
chai2010
2022-07-22 07:15:57 +08:00
看下来有 Go/Swift/Rust/C++ 的影子——总之没有眼前一亮的感觉(晃眼的特性不少)。
感觉本质还是 G 公司开始收割开源社区的韭菜,属于新瓶装老酒。

自己动手、丰衣足食,开始挖自己的语言坑:凹语言™ (Github: wa-lang/wa)
Dragonphy
2022-07-22 08:41:48 +08:00
@wsseo #103
说了好久了,还没出[哈哈]
opentrade
2022-07-22 12:03:50 +08:00
我喜欢
allgy
2022-07-22 15:15:11 +08:00
国内公司提升 kpi:造框架,google 提升 kpi:造语言,这就是差距
Hanggi
2022-07-22 17:06:12 +08:00
@imes
if err != nil 有什么问题吗?
laneagle
2022-08-23 18:18:19 +08:00
@duke807 最后 c++干翻碳语言

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://yangjunhui.monster/t/867631

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX