Carbon Language
Syntax and trade-offs
Jon Ross-Perkins
github.com/JonMeow
Successor language
Successor language
Successor language
Successor language
Successor language
🧪🧪🧪�UNDER�CONSTRUCTION
Language goals
Project goals
Between friends, there is room for disagreement.
“
“
Haim Saban
Project goals
C++ tooling
Carbon tooling
Tools require context
int a = 0, b = 1;
for (int i = 0; i < n; ++i) {
int c = a + b;
std::cout << b << "\n";
a = b;
b = c;
}
void Fibonacci(int n) {
int a = 0, b = 1;
for (int i = 0; i < n; ++i) {
int c = a + b;
std::cout << b << "\n";
a = b;
b = c;
}
}
What is it?
printer<circle>(radius);
It's a...
struct circle {};
template<typename T>
void printer(int radius) {}
auto main() -> int {
int radius = 3;
printer<circle>(radius);
return 0;
}
It's a...
#define radius 3
struct circle {};
template<typename T>
struct printer {
printer(float r) {}
};
auto main() -> int {
printer<circle>(radius);
return 0;
}
It's a...
constexpr int radius = 3;
struct circle {};
template<typename T>
struct printer {
void print() {}
};
auto main() -> int {
printer<circle>(radius);
radius.print();
return 0;
}
It's a...
auto main() -> int {
int printer = 1;
int circle = 2;
int radius = 3;
printer<circle>(radius);
return 0;
}
Context is king
Context is king
But should it be?
Why does context matter?
Why minimize context?
Common considerations
Want a:
Printer(Circle, radius);
Printer(Circle).Make(radius);
var radius: Printer(Circle);
printer < circle and circle > (radius);
Introducer keywords
var radius: Printer(Circle);
class Circle
class Printer(template t:! Type)
fn Draw()
interface Shape
let Pi: f32
Trade-offs
Templates
fn Printer(template T:! Type, radius: i32);
// Call:
Printer(Circle, radius);
class Printer(template T:! Type) {
fn Make(radius: i32) -> Printer(T);
}
// Construct instance:
Printer(Circle).Make(radius);
Trade-offs
auto Circle() -> int { return 0; }
struct Circle {};
auto main() -> int {
auto c = Circle();
}
auto Circle() -> int { return 0; }
struct Circle {};
template <typename T>
class Printer {};
auto main() -> int {
auto f = Circle{};
auto p = Printer<Circle>();
auto c = (struct Circle){};
}
❌
❌
✅
Operator associativity
// C++
printer<circle>(radius);
// Carbon
printer < circle and circle > radius;
// or
(if (printer < circle) then 1 else 0) > radius;
https://en.cppreference.com/w/cpp/language/operator_precedence�https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/expressions/README.md
C++
Carbon
Trade-offs
Packages and namespaces
package Sample api;
import Geometry;
namespace Draw;
fn Draw.Circle(c: Geometry.Circle) {}
Trade-offs
Tools require context
Between friends, there is room for disagreement.
“
“
Haim Saban
Jon Ross-Perkins
github.com/JonMeow
Thank you!
carbon.compiler-explorer.com
github.com/carbon-language-carbon-lang
#carbonlang