谷歌发布开源开发语言 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;
}
14803 次点击
所在节点    C++
110 条回复
FrankAdler
2022-07-20 21:29:43 +08:00
go:我吃醋了
nyxsonsleep
2022-07-20 21:29:45 +08:00
这 Carbon 的语法看起来很像他家的另一个语言。
Martens
2022-07-20 21:30:45 +08:00
有点 go 味儿
dbskcnc
2022-07-20 21:33:35 +08:00
只是个未画完成的饼, 不知道三年能不能下锅
secondwtq
2022-07-20 21:33:37 +08:00
看不出有啥亮点。但是支持 C++ Interop 很独特,这么多语言说要替代 C++,没一个把这个放进去的
我刚看到的时候还以为是 lattner 又出来了,仔细一看是 chandlerc ...
Leviathann
2022-07-20 21:38:06 +08:00
可以发篇公众号震惊体说 google 已经准备抛弃 go 了
churchill
2022-07-20 21:41:01 +08:00
碳语言比 C 语言好听。。。
agagega
2022-07-20 21:42:43 +08:00
https://www.reddit.com/r/programming/comments/w2thvo/comment/igs25eu/

发布这个语言的动机有点…苹果行为属于是。
nyxsonsleep
2022-07-20 21:42:53 +08:00
@churchill 碳中和警告
duke807
2022-07-20 21:57:07 +08:00
很好,支持 carbon 干翻 rust
duke807
2022-07-20 21:58:05 +08:00
然後再来一個新的干翻 carbon
kidlj
2022-07-20 22:00:42 +08:00
好事儿。可能就像 Kotlin 和 Java 的关系吧。
golangLover
2022-07-20 22:02:16 +08:00
编程语言之中 Google 发明 是不可信的代名词
DOLLOR
2022-07-20 22:03:43 +08:00
go: ?
dart: ?
TWorldIsNButThis
2022-07-20 22:14:05 +08:00
@duke807 官方评价:If you can use Rust, ignore Carbon
imes
2022-07-20 22:26:35 +08:00
@Martens 3# 我心惊胆战的看了一遍,幸好没有 if err != nil
thedrwu
2022-07-20 22:28:06 +08:00
c++历史包袱已经够多了,一股脑塞进一个 go 里 go 气的大包袱里
ksc010
2022-07-20 22:28:14 +08:00
之前 go 我记得也是要替代 C++的
nbndco
2022-07-20 22:29:11 +08:00
Google 的内部项目顺带开源了而已,有没有人用根本无所谓,Google 自己把自己的那堆内部的 C++慢慢替换掉就行了。和 hack 一样。
junkun
2022-07-20 22:41:25 +08:00
我还是很奇怪,既然想加新功能,又要兼容 C++,为什么不直接分叉一个 C++标准和编译器?是因为 g++名字已经被占用了吗。个人觉得,这又要兼容 C++,又发明一套不一样的语法挺无聊的。

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

https://www.v2ex.com/t/867631

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

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

© 2021 V2EX