谷歌发布开源开发语言 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;
}
14912 次点击
所在节点    C++
110 条回复
fgwmlhdkkkw
2022-07-21 09:47:42 +08:00
Google 跟“<>”有仇吗?
fgwmlhdkkkw
2022-07-21 09:48:35 +08:00
@yazinnnn #37 所谓英雄……
ttdx
2022-07-21 09:54:42 +08:00
go 里 go 气的
matrix67
2022-07-21 09:55:06 +08:00
✔ 赶紧去把这个语言的 json 库和 http 写了,github star 就归你了
paoqi2048
2022-07-21 09:56:02 +08:00
支持内嵌就有点意思
dqzcwxb
2022-07-21 09:59:34 +08:00
用 go 重新 java 和 c++项目,然后用 carbon 重写 go 项目,kpi 猛猛涨
elonlo
2022-07-21 10:00:14 +08:00
大胆去试吧,不行就换 java
YouRTBUG
2022-07-21 10:02:15 +08:00
@matrix67 不取名叫个 fast/rapid/crazy json 我都觉得不好意思
Huelse
2022-07-21 10:03:14 +08:00
@DOLLOR #32 个人觉得方括号是泛型的最佳选择,尖括号在 if 上用的太多,圆括号就更不用说了到处有,但实在不明白这个 carbon 为啥用圆括号
Mexion
2022-07-21 10:08:16 +08:00
谷歌开发的语言都很烂
iosyyy
2022-07-21 10:08:28 +08:00
@v23x 这和很多人饱受 C++之苦有啥关系? 逻辑都不通硬往上靠
murmur
2022-07-21 10:12:50 +08:00
老老实实做 go 不行么,这√b 语法
x1aoYao
2022-07-21 10:24:32 +08:00
@Huelse 泛型用方括号的我知道的有 Scala3, Go, 尖括号的基本是主流,圆括号的没听说过...(google 曾经想在 go 里用圆括号最终还是被否了, 现在又来了...)
fengjianxinghun
2022-07-21 10:30:38 +08:00
Google 能不能先学会解析<>再来搞编程语言?
libook
2022-07-21 10:31:25 +08:00
1. 不确定是官方口径还是媒体夸大;又见这种“替代论”,当前算是新技术宣传的必备话术了,当年 Go 也是号称要替代 C++的,Deno 号称替代 Node.js ,再往前倒 C#也曾称以替代 Java 为目标;蹲一个在 Chromium 和 AOSP 中的大量实践再看。
2. 在 CPP 的活动上宣传取代 CPP 的东西,虽然从宣传角度上来说很合理,但不知道在场的 CPP 开发者怎么看。当年某技术的线下聚会请来了一位“替代者”技术的开发者,对主办技术一痛踩以及对“替代者”技术的一阵吹,反正我是感觉挺尴尬的。
hakr
2022-07-21 10:31:25 +08:00
想问下 golang 当初是怎么流行起来的
newmlp
2022-07-21 10:33:52 +08:00
cpp 日常要被替代,结果没一个能打的
x1aoYao
2022-07-21 10:45:36 +08:00
如果 Swift 调用 cpp 能有调用 objc 那么顺滑就很方便了。看起来 Carbon 也是 import 了 clang 来解析 cpp
hsfzxjy
2022-07-21 10:56:16 +08:00
关于泛型用圆括号,我在 zig 里看到类似的做法。

zig 里类型是 first class 的,List :: type -> type 可以看成一个作用于类型的函数,List(int) 则看成一个函数调用。
https://ziglang.org/learn/overview/#generic-data-structures-and-functions

当然我没有说这种语法是易读的
charmToby
2022-07-21 11:05:02 +08:00
README 里面说了和 C++的关系。

JavaScript → TypeScript
Java → Kotlin
C++ → Carbon

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

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

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

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

© 2021 V2EX