谷歌发布开源开发语言 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 条回复
unnamedhao
2022-07-21 15:01:49 +08:00
@ligiggy 不知道有没有宏
ksedz
2022-07-21 15:10:39 +08:00
JavaScript → TypeScript
Java → Kotlin
C++ → Carbon

要是真能像他说的做到这个效果,我是很喜欢的。
linshenqi
2022-07-21 15:24:32 +08:00
从 faq 来看,也没说要替代谁。
Akitora
2022-07-21 15:36:44 +08:00
真 rust 和 go 偷偷生了二胎(一胎是 vlang )
zjsxwc
2022-07-21 15:53:56 +08:00
https://github.com/carbon-language/carbon-lang/blob/trunk/docs/project/faq.md#why-not-rust

如果你使用 rust 请继续使用 rust ,忽略 carbon ,rust 非常优秀,
而 carbon 仅仅是由于 c++与 rust 不能很好的互相操作(不是不等不写 unsafe rust ,就是得大量修改 c++之前的代码来符合 rust )而出现的代替方案。
allgy
2022-07-21 16:39:59 +08:00
数一数谷歌开发的语言黄了多少个
JingW
2022-07-21 16:54:03 +08:00
话说这个项目可以升职几个工程师?
nbndco
2022-07-21 19:12:24 +08:00
@yiqiao 为什么开源就是 KPI 项目? Google 内的 perf ,帮助整个公司 migrate off C++肯定比开源个没啥人用的语言要有 impact 的多的多的多的多。只是文化上 Google 就是很喜欢开放,能开源的东西基本都开源了。

但是明显这个只是顺带开源,没想着要有人用,也没人靠这种东西拿 OKR (可能万一做大了会有几个 developer advocate 的 role 吧)。Google 都已经说的这么明白了,我这东西是为了取代 C++,请大家去用 rust ,不要没事用这个项目,想拿 KPI 的人会说这个话么?
cmdOptionKana
2022-07-21 19:27:33 +08:00
@allgy Go 没黄,Dart 也没黄,谷歌还开发了什么语言?
abcd191898105
2022-07-21 19:28:23 +08:00
@golangLover 微软很强,大家也不敢上车啊
sosilver
2022-07-21 19:30:55 +08:00
假酒喝多了,设计成这样
aliveyang
2022-07-21 19:45:32 +08:00
希望国内大厂也可以至少折腾一下语言,动都不动跟躺平一样,哪怕是画一下呢
haolongsun
2022-07-21 19:49:22 +08:00
@aliveyang 人才储备都不一样,国内都是业务型大佬,真上升到底层语言级别的,能有多少个大佬。
iwdmb
2022-07-21 20:43:09 +08:00
又要產生變革了
iwdmb
2022-07-21 20:47:20 +08:00
If you can use Rust, ignore Carbon

If you want to use Rust, and it is technically and economically viable for your project, you should use Rust. In fact, if you can use Rust or any other established programming language, you should. Carbon is for organizations and projects that heavily depend on C++; for example, projects that have a lot of C++ code or use many third-party C++ libraries.

We believe that Rust is an excellent choice for writing software within the pure Rust ecosystem. Software written in Rust has properties that neither C++ nor Carbon have. When you need to call other languages from Rust, RPCs are a good option. Rust is also good for using APIs implemented in a different language in-process, when the cost of maintaining the FFI boundary is reasonable.

看來目標不是取代 Rust
FightPig
2022-07-21 21:03:55 +08:00
好奇谷歌为啥动不动就搞个新的,不去改进原来的,不是 kpi ?
cmdOptionKana
2022-07-21 21:25:47 +08:00
@FightPig 现有的语言,比如 C++,有委员会,谷歌不是想改就能改,没有主导权,要看别人脸色,推动起来太艰难了。
Valid
2022-07-21 21:34:07 +08:00
谷歌造语言造的真勤
iosyyy
2022-07-21 21:37:27 +08:00
@haolongsun 从来就没有业务和底层这种区别程序员大多都是全能的取决于能不能做罢了 倒不如说国内的核心在业务而不在"开源"
zhuweiyou
2022-07-21 21:39:33 +08:00
@cmdOptionKana Dart 黄了好多年 又炒起来的.

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

https://yangjunhui.monster/t/867631

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

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

© 2021 V2EX