谷歌发布开源开发语言 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;
}
14853 次点击
所在节点    C++
110 条回复
f64by
2022-07-20 22:46:49 +08:00
We also have explicit non-goals for Carbon, notably including:

A stable ABI for the entire language and library
Perfect backwards or forwards compatibility

就凭这两点,这门语言永远不可能成为主流
nbndco
2022-07-20 23:01:17 +08:00
@junkun 因为目标就是 migrate off C++,兼容是过程,目标是干掉 C++。就如同 swift 可以调用 objective-c 并不是因为 apple 太爱 objective-c 一样。
jeeyong
2022-07-20 23:05:15 +08:00
看来看去...还是老实啃 C++吧
luozic
2022-07-20 23:06:17 +08:00
google 的 dart 垃圾中的垃圾。还不如 java 6
WebKit
2022-07-20 23:19:28 +08:00
类似于 Java 与 Kotlin 的关系?其实我觉得挺好的。
buxudashi
2022-07-20 23:21:47 +08:00
搞个++c 吧。
SenLief
2022-07-20 23:24:16 +08:00
学习的速度都赶不上语言出来的速度
junkun
2022-07-20 23:31:36 +08:00
@nbndco 难道换成自己标准的 C++ 就不是 migrate off C++ 了吗?
edimetia3d
2022-07-20 23:32:48 +08:00
看了这个背景, 感觉为什么不去 fork clang 呢, 反正 carbon-lang 看起来也只是 llvm 的另一个前端,只换个前端真的能带来足够的性能收益吗?

@agagega
yohole
2022-07-20 23:37:16 +08:00
一言不合就是[取代]或者[替代],虽然 Google 有钱有人有推广能力,但是真的这么容易吗?
leimao
2022-07-20 23:57:57 +08:00
求求你们别瞎搞了好吗?多做点有意义的事不好吗
DOLLOR
2022-07-21 00:42:35 +08:00
我感觉 google 是不是跟尖括号有仇,这次居然用圆括号来表示泛型参数,看起来跟函数的参数列表长得一个样。将来他们怎么区分泛型函数调用和柯里化函数调用?
pengtdyd
2022-07-21 06:11:09 +08:00
以前是学习的速度跟不上框架,现在是跟不上语言了。。。。。。。
v23x
2022-07-21 07:14:24 +08:00
现在的新语言是真的多...

说明编译器发展得不错

也说明 C++确实很多人都饱受其苦
macha
2022-07-21 07:51:37 +08:00
这么多号称要取代 C++的语言都没能成功取代,历史遗留代码的力量不容小视。
yzbythesea
2022-07-21 08:05:35 +08:00
go 不是替代 cpp 的,性能还是有很大差距
yazinnnn
2022-07-21 08:50:36 +08:00
傻逼谷歌是不是跟尖括号有仇? 这咋还整了一个小括号的泛型
x500
2022-07-21 08:57:30 +08:00
google 这么多年,也末推出一个 android 上面的类 ios 的 c++原生开发,对它们有点失望
hatsuyuki
2022-07-21 09:03:17 +08:00
@v23x 有了 LLVM ,新语言就只需要考虑编写生成 LLVM IR 的前端,比以前容易太多了
zed1018
2022-07-21 09:37:50 +08:00
google 的语言一如既往的丑

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

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

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

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

© 2021 V2EX