charlieethan
V2EX  ›  C++

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

  •  
  •   charlieethan · Jul 20, 2022 · 18909 views
    This topic created in 1567 days ago, the information mentioned may be changed or developed.

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

    自己动手、丰衣足食,开始挖自己的语言坑:凹语言™ (Github: wa-lang/wa)
    Dragonphy
    Dragonphy
    Jul 22, 2022
    106
    @wsseo #103
    说了好久了,还没出[哈哈]
    opentrade
    opentrade
    Jul 22, 2022
    107
    我喜欢
    allgy
    allgy
    Jul 22, 2022
    108
    国内公司提升 kpi:造框架,google 提升 kpi:造语言,这就是差距
    Hanggi
    Hanggi
    Jul 22, 2022
    109
    @imes
    if err != nil 有什么问题吗?
    laneagle
    laneagle
    Aug 23, 2022
    110
    @duke807 最后 c++干翻碳语言
    1  2  
    ❮ ❯
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3707 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 05:23 · PVG 13:23 · LAX 21:23 · JFK 00:23
    ♥ Do have faith in what you're doing.