Code360 powered by Coding Ninjas X Code360 powered by Coding Ninjas X
Last Updated: Mar 27, 2024

Namespace and Alias in Carbon

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM


Are you bored learning the old programming languages Java, C++, etc.? Want to learn something new?

This article is focused on the new programming language Carbon. Carbon language is an experimental language. It is a general-purpose and open-source project. It was initiated by Google. It was first introduced by Chandler Carruth on 19 July 2022. He is a software engineer at Google. It was in Toronto at the CppNorth conference. Carbon language fixed many shortcomings of C++. It has many similarities to the features set of C++. It is the successor of C++. This language aims to make two-way interoperability and readability.

Now, let's dive into the article to learn about concepts of namespaces and aliasing in detail.

Namespace and Alias in Carbon


A namespace declaration is a name that can be used as a prefix for names declared later. Other namespace members are considered when defining a member of that namespace. They can be found without the namespace prefix using the name lookup. 

Namespaces give entities named paths. Nested namespaces are also possible. The same namespace may contain contributions from many libraries. In actuality, packages might have namespaces like Testing that contain entities that profit from a private place yet are found in numerous libraries.

The syntax of the namespace keyword can be described as a given regular expression:

namespace NAME_PATH;

Namespace entity is declared by namespace keyword. The given an example uses a namespace Ns1 to define some of the members of a package Pkg:

package Pkg api;

namespace Ns1;  // Defines namespace `Ns1` within this package.
namespace Ns2.S;    // Defines namespaces `Ns2` and `Ns2.S`.

fn A();
private fn Ns1.B(); //Declares function `B` in namespace `Ns1`.
fn NS.C(); //This will throw an error - `NS` hasn't been declared

fn D() {
  E();  // This will throw an error - No `package.E`

fn Ns1.F() {
  // Look in both `package.Ns1` and `package`.
  A(); // Finds `package.A` and `package.Ns1.B`.

fn Ns2.S.G();   //Declares function `G` in namespace `Ns2.S`.
fn Ns2.H();     //Declares function `H` in namespace `Ns2`.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job

Re-declaring imported namespaces

Along with being declared in the current file, namespaces can exist on entities from imported packages. However, even if the namespace is present in an imported library from the current package, it must still be declared locally to add symbols. 

A different package that imports Pkg may use the prefix Pkg followed by the namespace Ns1 to refer to the public members of that namespace:

import Pkg;

Pkg.A();        // `A` is a public member of `Pkg`.
Pkg.Ns1.B();    // This will throw an error - `Ns1.B` is a private member of `Pkg`.
Pkg.Ns1.F();    //`Ns1.F` is a public member of `Pkg`.
Pkg.Ns2.S.G();  //`Ns2.S.G` is a public member of `Pkg`.
Pkg.Ns2.H();    //`Ns2.H` is a public member of `Pkg`.


Alias means defining another name for a name. 

alias newName = PackageName.oldName;

Note: Name is present on the equal to sign (=) right-hand side. It is not a value. So alias five = 5 is not correct. 

Alias is a Carbon keyword that helps in aliasing namespaces. As a result, aliases can interact with entities like namespaces that aren't valued in Carbon.

For example, 

namespace Timezones.Internal;
alias T = Timezones.internal;

struct T.Raw_Data { ... }
fn ParseData(data: T.Raw_Data);

When updating a name, this can be applied during an incremental migration. For example, aliases would let you have two names for the same data field in a class. Even when clients were switched between the two names. 

class CN {
    var newName: String;
    alias oldName = newName;

var x: CN = {.newName = "CodingNinjas"};
Carbon.Assert(x.oldName == "CodingNinjas");

It is also used to include a name in the public API. For example, a name may be renamed from an interface implementation. It could be included as a class member or named constraint using the alias keyword.

Frequently Asked Questions

What is an alias in a namespace?

Namespace aliases help programmers define an alternate name for any namespace. Aliases are used as a convenient shortcut for extended or deeply-nested namespaces.

What is the difference between a name and a namespace?

Names are like "variables," whereas Namespaces are the locations where names stay.

What is the advantage of a namespace?

A namespace is a part where the identifiers' scope, like functions, the name of types, variables, classes, etc., are declared. Generally, the code has multiple libraries and namespaces. This helps avoid ambiguity when two identifiers have the same name.

Is Carbon replacing C++?

Despite popular belief, Google's open-source Carbon programming language is not a strict replacement for C++. Instead, it is designed as a successor. While this may sound the same, the critical difference is that its purpose is to be used alongside C++.


In this article, we have discussed the concepts of namespace and alias in the Carbon programming language. We learned the implementation of namespace and aliases.

We hope this blog has helped you enhance your Carbon language knowledge. Check out our articles to learn more about variables in Carbon and data types in Carbon

Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

thank you image
Topics covered
Re-declaring imported namespaces
Frequently Asked Questions
What is an alias in a namespace?
What is the difference between a name and a namespace?
What is the advantage of a namespace?
Is Carbon replacing C++?