Carbon is yet another programming language made by Google.
They say it’s like a last resort for projects that must keep C++, so its only goal is to
not suck as much as C++.
If you can tolerate dealing with cross-language boundaries, then they recommend whatever language
everyone’s worshipping these days: probably still Rust.
Obviously, that means Carbon will kill C++.
The README has instructions for installing Carbon.
If you’re not a true bloatware enjoyer, have GitHub build it for you.
Make a fork of the Carbon repository, link in the description.
If you now want bloatware, press the period key to open github.dev to edit the repository
in a VS Code browser instance.
Create a new YAML file in .github/workflows, and name it whatever you want.
I put a link to the workflow file in the description.
It creates a workflow that only runs when you tell it to.
I’m using WSL with Ubuntu, so I’m running the workflow on Ubuntu latest.
Carbon only supports based operating systems, like not-Windows.
Git checkout the repo.
Have Python 3.9 installed.
Install Bazelisk and LLVM using Homebrew—it must be through Homebrew, or the build process
will throw a hissy fit at the end.
Run “bazel build //explorer,” and upload the result to GitHub for your enjoyment.
Inside the zip file is the Carbon explorer binary.
In your working directory, you also need to download prelude.carbon—link in the description—and
put it in explorer/data.
Otherwise, the compiler will die trying to find it.
I recommend putting the binary inside explorer as well.
Carbon uses just-in-time compilation.
To run a Carbon program, type the path to the explorer binary, such as ./explorer/explorer,
followed by the Carbon program path.
Isn’t this a lot easier than using a preinstalled gcc?
All Carbon programs must start with a “package” keyword, followed by the name of a language
you want to make fun of, the “API” keyword, then a semicolon because Carbon is a based
programming language.
To create a function, use the “fn” keyword, followed by the name.
Don’t forget to capitalize “Main.”
Add parentheses containing the parameters.
Main doesn’t have any.
Then, add an arrow for the return type.
Main must return an i32.
After the return type, surround the function body with curly braces.
Inside, call the Print function as you would in Java.
String literals must use double quotes.
Don’t forget the semicolon!
Ah, it appears I forgot to return an i32 value, even though I said I would.
Carbon prevents you from making silly mistakes like these.
Add a return 0 statement to the end of Main.
Congratulations!
You’ve written your first Carbon program and have unlocked junior Carbon developer
status.
A reasonable next step for Carbon developers is to create a React app with a clicker game.
A more practical alternative would be a to-do list.
Carbon does not support unsafe operations such as writing to a file, so this program
will be responsible for printing an executable into standard output.
I do not want my program to be misused by bad actors, so I will make the program password-protected.
First, comment out your existing program.
Carbon does not support multiline comments because they are seldom used in practice.
Write a new function named CreateReactApp.
It will take in a parameter, password, of type string.
Inside the parentheses, type the parameter name, a colon, then the type.
String is capitalized.
CreateReactApp returns an exit code depending on whether the given password is correct.
Ignoring my initial implementation here, write Main.
It calls CreateReactApp and passes in the password, then returns its return value.
This is a tail call, so it is eligible for tail call optimization.
Carbon probably does not have tail call optimization.
Carbon does not accept my naïve implementation of CreateReactApp because Carbon promotes
good programming practices and does not permit the use of a not-equals operator.
Use an if statement, which in Carbon involves parentheses around the condition and curly
braces around its body, to only print the React app if the password is correct.
Carbon lets you compare string contents using a double equals.
The program correctly rejects an incorrect password.
Nice try, hacker!
Inside the if statement, create a variable using the var keyword, followed by the variable
name, a colon, and its type—here, a string—then a semicolon.
Initialize it by inserting an equals sign followed by its value before the semicolon.
To make the code cleaner, I use a string block literal using triple double quotes so I can
neatly have a string containing multiple lines.
I annotate that the string contains HTML by adding a file type indicator after the opening
triple quotes—this is a rare language feature that I have only seen before in Markdown.
After the variable declaration, call Print and pass in the app variable.
Inside the string, type out code for a basic React app, which should be trivial for the
average C++ developer.
You can have some creativity here.
I took the liberty not to.
Note that tab characters are not permitted in string literals because Carbon promotes
the Google style guide for JavaScript with 2-space indentation, and opening curly brackets
must be escaped by typing them twice.
In Carbon, a single opening curly bracket in a string is used to crash the program.
Pipe the output of your program into an HTML file.
You now have a Carbon program capable of producing a working React app.
Even GitHub Copilot struggles with this, sometimes.
Congratulations, you have now attained 10 years’ worth of Carbon developer experience.