V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
charlieethan
V2EX  ›  C++

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

  •  
  •   charlieethan · 2022-07-20 21:22:10 +08:00 · 14788 次点击
    这是一个创建于 642 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目的 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;
    }
    
    110 条回复    2022-08-23 18:18:19 +08:00
    1  2  
    XIVN1987
        101
    XIVN1987  
       2022-07-21 22:15:52 +08:00
    虽然好多人不看好 Carbon ,但考虑到 Google 的超能力,,说不定比 Rust 更流行也未可知。。
    w3cll
        102
    w3cll  
       2022-07-21 22:32:09 +08:00
    Google 都有好多个语言了,能不能把一个搞好
    wsseo
        103
    wsseo  
       2022-07-21 22:53:29 +08:00
    没意思,期待华为的语言
    Aloento
        104
    Aloento  
       2022-07-22 05:46:00 +08:00
    考虑一下 Zig 吧
    chai2010
        105
    chai2010  
       2022-07-22 07:15:57 +08:00
    看下来有 Go/Swift/Rust/C++ 的影子——总之没有眼前一亮的感觉(晃眼的特性不少)。
    感觉本质还是 G 公司开始收割开源社区的韭菜,属于新瓶装老酒。

    自己动手、丰衣足食,开始挖自己的语言坑:凹语言™ (Github: wa-lang/wa)
    Dragonphy
        106
    Dragonphy  
       2022-07-22 08:41:48 +08:00
    @wsseo #103
    说了好久了,还没出[哈哈]
    opentrade
        107
    opentrade  
       2022-07-22 12:03:50 +08:00
    我喜欢
    allgy
        108
    allgy  
       2022-07-22 15:15:11 +08:00
    国内公司提升 kpi:造框架,google 提升 kpi:造语言,这就是差距
    Hanggi
        109
    Hanggi  
       2022-07-22 17:06:12 +08:00
    @imes
    if err != nil 有什么问题吗?
    laneagle
        110
    laneagle  
       2022-08-23 18:18:19 +08:00
    @duke807 最后 c++干翻碳语言
    1  2  
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5412 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 08:33 · PVG 16:33 · LAX 01:33 · JFK 04:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.